How to get 'this' value of caller function?
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'
pimvdb
Updated on June 28, 2022Comments
-
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 ofbar
gets logged.However, for this to work I have to pass the
this
in thebar.prototype.func
function. I was wondering whether it is possible to obtain the samethis
value without passingthis
.I tried using
arguments.callee.caller
, but this returns the prototype function itself and not thethis
value inside the prototype function.Is it possible to log the
test
instance ofbar
by only callingfoo()
in the prototype function? -
pimvdb almost 13 yearsThanks but I still need to pass the
this
value. What I mean is that I should be able to obtainthis
insidefoo
, by just callingfoo()
insidefunc
. -
KooiInc almost 13 yearsBut ehr, that's what happens here, or did I misunderstood your question? Calling foo like in my answer makes
this
from instancetest
available to foo, as is demonstrated by logging the testVal property of it vialog
. -
pimvdb almost 13 yearsI'm really sorry for not being clear. I meant obtaining the
this
offunc
withinfoo
without actually passingthis
infunc
. -
KooiInc almost 13 yearsOk, I don't really see the use of that, but hey. Actually
foo.call(this)
doesn't passthis
(as you can see,foo
has no parameters, nothing is passed), but sets the context offoo
tothis
, being the instance created by thebar
constructor -
Raynos almost 13 yearsYour still passing
this
around. I findfoo.apply(this)
to be a lot cleaner / less hackish. -
Scherbius.com almost 13 yearsI'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 almost 13 years@Raynos: That's correct, but I accepted because of stating that's it not possible. That basically answers my question.
-
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 almost 13 years@Raynos: I have to disagree. I recommend you to read wikipedia about what is called hack ;) Topic is closed.
-
KooiInc almost 13 years@Dmitriy Naumov topic is closed - so you're the authority, and everone else shut up?