开发者

Auto overriding context in javascript functions

Take in consideration this code:

function a() {
  alert(this.variable);
}

b = new function() {
  this.variable = "abc";
  a.call(this);
}

Is there a way to auto override context instead of using the call method? like this (not working):

function a() {
  var _this = Function.caller;  
  alert(_this.variable);
}

b = new function() {
  this开发者_运维百科.variable = "abc;
  a();
}

Thanks in advance.


If you want a to have access to b's this, you'll have to pass this explicitly, i.e. instead of a() do a(this).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜