如何获得'这个'来电函数的值? [英] How to get 'this' value of caller function?

查看:79
本文介绍了如何获得'这个'来电函数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的功能:

$ p $ function foo(_this){
console.log(_this );


函数bar(){}
bar.prototype.func = function(){
foo(this);
}

var test = new bar();
test.func();

然后 test <$ c的实例

然而,为了这个工作,我必须通过 this 中的 bar.prototype.func 函数。我在想,如果没有传递 this ,是否有可能获得相同的 this



我尝试使用 arguments.callee.caller ,但是这会返回原型函数本身,而不是<$ c

可以记录 test 这个通过在原型函数中只调用 foo()来实现 bar 的实例吗?

解决方案

如果问题是没有通过这个(通过任何方式),那么答案是

值可以通过替代方法传递。例如使用全局变量(在Bar类中)或者会话或者cookie。

  {

var myThis;

函数foo(){
console.log(myThis);
}

bar.prototype.func = function(){

myThis = this;
foo();
}
}

var test = new bar();
test.func();


If I have a function like this:

function foo(_this) {
    console.log(_this);
}

function bar() {}
bar.prototype.func = function() {
    foo(this);
}

var test = new bar();
test.func();

then the test instance of bar gets logged.

However, for this to work I have to pass the this in the bar.prototype.func function. I was wondering whether it is possible to obtain the same this value without passing this.

I tried using arguments.callee.caller, but this returns the prototype function itself and not the this value inside the prototype function.

Is it possible to log the test instance of bar by only calling foo() in the prototype function?

解决方案

If the question is 'without passing this (by any means)' then answer is no

value can be passed by alternative methods though. For example using global var (within Bar class) or session or cookies.

    function bar() {

      var myThis;

      function foo() {
          console.log(myThis);
      }

      bar.prototype.func = function() {

          myThis = this;
           foo();
      }
   }

   var test = new bar();
   test.func();

这篇关于如何获得'这个'来电函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆