How to get 'this' value of caller function?

只愿长相守 提交于 2019-12-21 09:24:27

问题


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?


回答1:


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();



回答2:


I think calling foo within the context of bar should work:

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'



回答3:


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();



回答4:


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 this property on a function you call with the method apply or call. See this reference for an explanation on this.

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

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

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

Usually if this is 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.

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



来源:https://stackoverflow.com/questions/6332017/how-to-get-this-value-of-caller-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!