在此示例中:
var A = {test: 1, foo: function() { return this.test }}为什么A.foo()返回1 (至少在node.js中)?我以为this会绑定到外部调用者this,不是吗?
发布于 2013-03-20 11:40:57
在使用obj.method()表示法的Javascript whenever you call a function中,this将绑定到obj。
您可以通过将呼叫拆分为两个单独的步骤来解决此问题:
var f = A.foo;
f(); // "this" will not be A in this case.或者,滥用逗号运算符:
(17, x.f)()https://stackoverflow.com/questions/15514846
复制相似问题