【发布时间】:2016-08-30 09:14:31
【问题描述】:
我已经阅读了有关在对象外部的函数内部使用绑定来访问 this 的信息,但由于某些原因,我无法做到这一点。
function test (len){
return this.length - len;
}
var SETTINGS = {
length : 21,
trimmed: test.bind(this,5),
x: function(){
return this.length;
}
};
SETTINGS.x(); // this is available.
SETTINGS.trimmed(); //len is 5 but this is Window.
可能和我对bind的理解有关。
【问题讨论】:
-
在定义
SETTINGS对象时this指向window
标签: javascript object call bind apply