Bind this argument to named argument
本问题已经有最佳答案,请猛点这里访问。
如果我的代码结构如下:
1 2 3 4 5 6 7 8 9 10 11 12 | Thing.prototype = { doSomething: function() { $(selector).click(this.handleClick.bind(null, this)); }, handleClick: function(self, event) { console.log(this); //Window console.log(self); //Thing } } |
如何将 Thing
注意:我知道我可以绑定
我希望我能完成我想要实现的目标,如果有任何不明确的地方,请发表评论。
How could one bind the Thing this context to the self argument and still keep the behavior of the this object as if no arguments were bound using the bind method?
您不会为此使用
1 2 3 4 5 6 | doSomething: function() { var self = this; $(selector).click(function(e) { return self.handleClick.call(this, self, e); }); }, |
在
1 2 3 4 5 | handleClick: function(self, event) { console.log(this); // The clicked element console.log(self); // The Thing console.log(event); // The event } |