javascript 如何获得调用者函数的“this”值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6332017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 20:19:02  来源:igfitidea点击:

How to get 'this' value of caller function?

javascriptfunctionscopethis

提问by pimvdb

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 testinstance of bargets logged.

然后记录的test实例bar

However, for this to work I have to pass the thisin the bar.prototype.funcfunction. I was wondering whether it is possible to obtain the same thisvalue withoutpassing this.

但是,要使其正常工作,我必须thisbar.prototype.func函数中传递。我想知道是否有可能在通过的情况下获得相同的this值。this

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

我尝试使用arguments.callee.caller,但这返回原型函数本身而不是this原型函数内的值。

Is it possible to log the testinstance of barby only calling foo()in the prototype function?

是否可以仅通过调用原型函数来记录test实例?barfoo()

采纳答案by Scherbius.com

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.

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

    function bar() {

      var myThis;

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

      bar.prototype.func = function() {

          myThis = this;
           foo();
      }
   }

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

回答by KooiInc

I think calling foowithin the context of barshould work:

我认为foo在上下文中调用bar应该有效:

function foo() {
    console.log(this.testVal);
}

function bar() { this.testVal = 'From bar with love'; }
bar.prototype.func = function() {
    foo.call(this);
}

var test = new bar();
test.func(); //=> 'From bar with love'

回答by Samuel Nakombi

What about this?

那这个呢?

"use strict";
var o = {
    foo : function() {
        console.log(this);
    }
}

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

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

Or this:

或这个:

"use strict";
Function.prototype.extender = function( o ){
    if(typeof o ==  'object'){
        this.prototype = o;
    }else if ( typeof o ==  'function' ) {
        this.prototype = Object.create(o.prototype);
    }else{
        throw Error('Error while extending '+this.name);
    }
    this.prototype.constructor = this;
}
var o = {
    foo : function() {
        console.log(this);
    }
}

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

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

回答by Samuel Nakombi

You can do this without changing the external function, but you must change the way you call it.

您可以在不更改外部函数的情况下执行此操作,但是您必须更改调用它的方式。

You can't get the context of the caller, but you can set the thisproperty on a function you call with the method applyor call. See this referencefor an explanation on this.

您无法获取调用者的上下文,但可以this在使用方法applycall 调用的函数上设置属性。有关的说明,请参阅此参考资料this

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

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

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

Usually if thisis used, it's in an object oriented context. Trying to call a method of an object with another this might indicate poor design. Explain a bit more what you are trying to achieve for more applicable design patterns.

通常如果this使用,它是在面向对象的上下文中。尝试使用另一个 this 调用对象的方法可能表明设计不佳。多解释一下您要为更适用的设计模式实现的目标。

For an example of a javascript OOP paradigm, check my answer here.

有关 javascript OOP 范例的示例,请在此处查看我的答案