1
在 [MDN] [绑定]使用绑定功能进行二次
我们看到这种类似的代码
this.x = 9; // this refers to global "window" object here in the browser
var module = {
x: 81,
getX: function() { return this.x; }
};
module.getX(); // 81
var retrieveX = module.getX;
retrieveX();
// returns 9 - The function gets invoked at the global scope
var boundGetX = retrieveX.bind(module);
boundGetX();
是的,我知道的绑定将改变“这个”价值,但我迷惑代码如下
var demo=boundGetX.bind(this);// or boundGetX.bind(window);
demo();// still 81
再次使用绑定,我认为它会改变“this”指向“窗口”,但它不是。 我不知道为什么。
'boundGetX'将是一个函数,它调用模块上的retrieveX,而不管它接收到了什么'this'值。现在,如果你再次“绑定”,那么'window'就会被忽略。 – Bergi
必须有一个副本,对吧?任何人? – Bergi