最佳答案
考虑一下这个简单的代码:
"use strict";
var obj = {
f: function() {
this.prop = 'value';
g.bind( this )();
}
};
function g() {
console.log( this.prop );
}
如果我尝试验证这段代码,jshint 会显示错误 Possible strict violation.
,在这里我调用 console.log( this.prop );
。这是因为 this
在函数的严格模式下是未定义的。
但是我在调用它之前绑定了这个函数,所以 this
是正确的对象。
我使用这种“设计模式”来避免主要对象混乱。在参数中传递属性也会使函数变得混乱,因此我拒绝这样做。而且,这正是 bind
的作用。
JSHint 有办法让我这么做吗?