1

I have the function:

$(this.myObject).click(A.bind(this));
function A(){
     //  stuff here
}

I want to call from within a second function (binding this).

$(this.myOtherObject).click(B.bind(this));
function B(){
      A();             //  works but does not bind
      A.bind(this);    //  does not work
}

How can I do this?

4
  • bind does not call/execute the method.... Commented Aug 23, 2018 at 15:16
  • Please check the MDN documentation for bind: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… . If you want to call the function, either use .call or .apply, by providing this as the first argoment of either two. Otherwise, use .bind, then call the function explicitely. Commented Aug 23, 2018 at 15:18
  • this.myObject belongs to global window object? Commented Aug 23, 2018 at 15:19
  • bind() return a function to be executed later. let X = A.bind(this); X(); Commented Aug 23, 2018 at 15:21

2 Answers 2

1

You want to use call(this) not bind() since bind() just returns the method.

A.call(this)
Sign up to request clarification or add additional context in comments.

Comments

0
function B(){
    A();             //  works but does not bind
    A.bind(this);    //  does not work
}

bind is for creating a new function bound to a context (see bind documentation). If you go that way, you would need to call the new function :

A.bind(this)();

However, you should rather use call method or apply method which will directly call the function with a specific context :

A.call(this);
A.apply(this);

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.