How to get 'this' value of caller function

2019-04-06 05:16发布

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?

4条回答
姐就是有狂的资本
2楼-- · 2019-04-06 05:39

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.

查看更多
做个烂人
3楼-- · 2019-04-06 05:41

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'
查看更多
Ridiculous、
4楼-- · 2019-04-06 05:49

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();
查看更多
Fickle 薄情
5楼-- · 2019-04-06 05:52

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();
查看更多
登录 后发表回答