如果我有一个这样的函数:
function foo(_this) {
console.log(_this);
}
function bar() {}
bar.prototype.func = function() {
foo(this);
}
var test = new bar();
test.func();
那么test
的实例bar
被记录。
但是,为了使其发挥作用,我必须通过this
in the bar.prototype.func
功能。我想知道是否有可能获得相同的this
value without通过this
.
我尝试使用arguments.callee.caller
,但这返回原型函数本身而不是this
原型函数内的值。
是否可以记录test
的实例bar
仅通过调用foo()
在原型函数中?
如果问题是“没有通过这个(以任何方式)”那么答案是no
不过,可以通过其他方法传递值。例如使用全局变量(Bar 类内)或会话或cookie。
function bar() {
var myThis;
function foo() {
console.log(myThis);
}
bar.prototype.func = function() {
myThis = this;
foo();
}
}
var test = new bar();
test.func();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)