Javascript函数绑定重写(如何将其绑定到另一个对象) [英] Javascript function bind override (how to bind it to another object)
问题描述
是否可以通过Function.prototype.bind重新绑定已经绑定到另一个对象的函数?
Is there a way to rebind a function that is already bound to another object via Function.prototype.bind?
var a={};
var b={};
var c=function(){ alert(this===a); };
c(); // alerts false
c=c.bind(a);
c(); // alerts true
c=c.bind(b);
c(); // still alerts true
我知道我可以使用其他方法并为绑定保留一个干净"的函数,但是我只是想知道如何重用已经绑定的函数.
I know that I can use a different approach and keep a "clean" function for binding, but I just wonder how to reuse an already bound function.
推荐答案
是否可以通过Function.prototype.bind重新绑定已经绑定到另一个对象的函数?
Is there a way to rebind a function that is already bound to another object via Function.prototype.bind?
不.来自 ES2015关于Function.prototype.bind
的规范:
No. From the ES2015 spec about Function.prototype.bind
:
19.2.3.2 Function.prototype.bind(thisArg,... args)
[...]
注2:如果 Target 是箭头函数或绑定函数,则随后传递给 F .
Note 2: If Target is an arrow function or a bound function then the thisArg passed to this method will not be used by subsequent calls to F.
早期版本也是如此.
这篇关于Javascript函数绑定重写(如何将其绑定到另一个对象)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!