javascript - Bind this argument to named argument -


this question has answer here:

in case have code structure follows :

 thing.prototype = {      dosomething: function() {       $(selector).click(this.handleclick.bind(null, this));     },      handleclick: function(self, event) {       console.log(this); //window       console.log(self); //thing     }   } 

how 1 bind thing this context self argument , still keep behavior of this object if no arguments bound using bind method?

note : know can bind this , use e.originalevent.target behaviour want i'm curious if there way

i hope i'm getting through want achieve, leave comment if ambiguous.

how 1 bind thing context self argument , still keep behavior of object if no arguments bound using bind method?

you wouldn't use bind that, because want this dynamic. instead:

dosomething: function() {   var self = this;   $(selector).click(function(e) {       return self.handleclick.call(this, self, e);   }); }, 

during handleclick, this refer element clicked, first argument thing instance, , second event:

handleclick: function(self, event) {   console.log(this);  // clicked element   console.log(self);  // thing   console.log(event); // event } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -