我有一个简单的 JSFiddle here展示我的问题。
我有这段 JavaScript 代码:
var b = document.getElementById("b");
function A() {
this.f = "1";
}
A.prototype.t = function() {
b.innerHTML = this.f;
};
var a = new A();
var l = a.t;
l();
为什么当我尝试调用 a.t 时 this
未定义?如何在不过于冗长或存储过多的情况下恢复该上下文?
Why is this undefined when I try to call a.t?
因为在 JavaScript 中,this
主要由调用函数的方式设置,而不是定义函数的位置。 a.t()
在调用中将 this
设置为 a
,但是 l()
设置了 this
为 undefined
(在严格模式下)或全局对象(在松散模式下)。
更多(在我的博客上):
唯一的异常(exception)是“绑定(bind)”函数(如 Function#bind
)或 ES6 的“箭头”函数(从它们所在的上下文中获取它们的 this
重新定义)。
How do I recover that context without being overly verbose or storing too much?
Function#bind
通常是一个很好的答案:
var l = a.t.bind(a);
l();
它返回一个新函数,当被调用时,调用原始函数并将 this
设置为您给 bind
的第一个参数。 (您也可以绑定(bind)其他参数。)它是一个 ES5 函数,但如果您需要支持真正老式的浏览器,您可以轻松地对其进行 polyfill。
如果您只需要使用特定的this
值调用 l
,而不总是让它使用该值,如Robert Rossmann points out您可以使用 Function#call
或 Function#apply
:
l.call(this, 'a', 'b', 'c'); // Calls `l` with `this` set to `a` and args 'a', 'b', and 'c'
l.apply(this, ['a', 'b', 'c']); // Calls `l` with `this` set to `a` and args 'a', 'b', and 'c' -- note they're specified in an array