开发者

Auto overriding context in javascript functions

开发者 https://www.devze.com 2023-01-25 02:32 出处:网络
Take in consideration this code: function a() { alert(this.variable); } b = new function() { this.variable = \"abc\";

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

精彩评论

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