pim*_*vdb 9 javascript scope function this
如果我有这样的功能:
function foo(_this) {
console.log(_this);
}
function bar() {}
bar.prototype.func = function() {
foo(this);
}
var test = new bar();
test.func();
Run Code Online (Sandbox Code Playgroud)
然后记录的test实例bar.
然而,对于这个工作,我需要传递this的bar.prototype.func功能.我想知道是否可以在不通过的情况下获得相同的this值.this
我尝试使用arguments.callee.caller,但这会返回原型函数本身,而不是原型函数中的this值.
是否可以通过仅调用原型函数来记录test实例?barfoo()
如果问题是“没有通过这个(以任何方式)”,那么答案是否定的
但是可以通过其他方法传递值。例如使用全局变量(在 Bar 类中)或会话或 cookie。
function bar() {
var myThis;
function foo() {
console.log(myThis);
}
bar.prototype.func = function() {
myThis = this;
foo();
}
}
var test = new bar();
test.func();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10273 次 |
| 最近记录: |