1

I want to run a function within an object on click. In the object's init method, I bind a click to "runBFS".

I use bind(this) to traversal so I can pass in the context of the object and not lose it to the window.

var api = {},
api.init = function () {
  document.getElementById("runBFS").addEventListener(
    "click",
    this.traversal.bind(this),
    false
  );
}

I need to pass in a boolean value on click (traverseNodesBool) but don't know how to pass it in when using bind()

api.traversal = function (e, traverseNodesBool) {

How do I do this?

2 Answers 2

1

Pass it as the second param to bind.

document.getElementById("runBFS").addEventListener("click", this.traversal.bind(this, traverseNodesBool);

api.traversal = function(yourBool, e) {}
Sign up to request clarification or add additional context in comments.

Comments

1

.bind(thisArg[, arg1[, arg2[, ...]]]) ,so first parameter is context and rest follows your other parameters, so it would be:

this.traversal.bind(this, yourParamHere);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.