如何获得'这个'来电函数的值? [英] How to get 'this' value of caller function?
问题描述
如果我有这样的功能:
$ p $ function foo(_this){
console.log(_this );
函数bar(){}
bar.prototype.func = function(){
foo(this);
}
var test = new bar();
test.func();
然后 test
<$ c的实例
然而,为了这个工作,我必须通过 this
中的 bar.prototype.func
函数。我在想,如果没有传递 this
,是否有可能获得相同的 this
值。
我尝试使用 arguments.callee.caller
,但是这会返回原型函数本身,而不是<$ c
可以记录 test $ c $>这个
通过在原型函数中只调用 foo()
来实现 bar
的实例吗?
如果问题是没有通过这个(通过任何方式),那么答案是否 值可以通过替代方法传递。例如使用全局变量(在Bar类中)或者会话或者cookie。 If I have a function like this: then the However, for this to work I have to pass the I tried using Is it possible to log the 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.
这篇关于如何获得'这个'来电函数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
{
var myThis;
函数foo(){
console.log(myThis);
}
bar.prototype.func = function(){
myThis = this;
foo();
}
}
var test = new bar();
test.func();
function foo(_this) {
console.log(_this);
}
function bar() {}
bar.prototype.func = function() {
foo(this);
}
var test = new bar();
test.func();
test
instance of bar
gets logged.this
in the bar.prototype.func
function. I was wondering whether it is possible to obtain the same this
value without passing this
.arguments.callee.caller
, but this returns the prototype function itself and not the this
value inside the prototype function.test
instance of bar
by only calling foo()
in the prototype function? function bar() {
var myThis;
function foo() {
console.log(myThis);
}
bar.prototype.func = function() {
myThis = this;
foo();
}
}
var test = new bar();
test.func();