How to get 'this' value of caller function?

12,586

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

Solution 2

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

Solution 3

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'
Share:
12,586
pimvdb
Author by

pimvdb

Updated on June 28, 2022

Comments

  • pimvdb
    pimvdb almost 2 years

    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?

  • pimvdb
    pimvdb almost 13 years
    Thanks but I still need to pass the this value. What I mean is that I should be able to obtain this inside foo, by just calling foo() inside func.
  • KooiInc
    KooiInc almost 13 years
    But ehr, that's what happens here, or did I misunderstood your question? Calling foo like in my answer makes this from instance test available to foo, as is demonstrated by logging the testVal property of it via log.
  • pimvdb
    pimvdb almost 13 years
    I'm really sorry for not being clear. I meant obtaining the this of func within foo without actually passing this in func.
  • KooiInc
    KooiInc almost 13 years
    Ok, I don't really see the use of that, but hey. Actually foo.call(this) doesn't pass this (as you can see, foo has no parameters, nothing is passed), but sets the context of foo to this, being the instance created by the bar constructor
  • Raynos
    Raynos almost 13 years
    Your still passing this around. I find foo.apply(this) to be a lot cleaner / less hackish.
  • Scherbius.com
    Scherbius.com almost 13 years
    I'm not passing it around, but setting value to a variable that is accessible within the namespace of the Bar class. That is a good and right way to do it.
  • pimvdb
    pimvdb almost 13 years
    @Raynos: That's correct, but I accepted because of stating that's it not possible. That basically answers my question.
  • Raynos
    Raynos almost 13 years
    @DmitriyNaurnov your not passing it around through function arguments, your passing it around through bar local scope and it's a hack
  • Scherbius.com
    Scherbius.com almost 13 years
    @Raynos: I have to disagree. I recommend you to read wikipedia about what is called hack ;) Topic is closed.
  • KooiInc
    KooiInc almost 13 years
    @Dmitriy Naumov topic is closed - so you're the authority, and everone else shut up?