考虑以下代码:
function a() {
alert(this.variable);
}
b = new function() {
this.variable = "abc";
a.call(this);
}
有没有一种方法可以自动覆盖上下文而不是使用call方法?
像这样(不起作用):
function a() {
var _this = Function.caller;
alert(_this.variable);
}
b = new function() {
this.variable = "abc;
a();
}
提前致谢。
最佳答案
如果希望a
可以访问b
的this
,则必须显式地传递this
,即,代替a()
执行a(this)
。