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
Post a Comment