javascript 如何获得调用者函数的“this”值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6332017/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get 'this' value of caller function?
提问by pimvdb
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.
然后记录的test
实例bar
。
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 withoutpassing this
.
但是,要使其正常工作,我必须this
在bar.prototype.func
函数中传递。我想知道是否有可能在不通过的情况下获得相同的this
值。this
I tried using arguments.callee.caller
, but this returns the prototype function itself and not the this
value inside the prototype function.
我尝试使用arguments.callee.caller
,但这返回原型函数本身而不是this
原型函数内的值。
Is it possible to log the test
instance of bar
by only calling foo()
in the prototype function?
是否可以仅通过调用原型函数来记录test
实例?bar
foo()
采纳答案by Scherbius.com
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.
但是可以通过其他方法传递值。例如使用全局变量(在 Bar 类中)或会话或 cookie。
function bar() {
var myThis;
function foo() {
console.log(myThis);
}
bar.prototype.func = function() {
myThis = this;
foo();
}
}
var test = new bar();
test.func();
回答by KooiInc
I think calling foo
within the context of bar
should work:
我认为foo
在上下文中调用bar
应该有效:
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'
回答by Samuel Nakombi
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();
回答by Samuel Nakombi
You can do this without changing the external function, but you must change the way you call it.
您可以在不更改外部函数的情况下执行此操作,但是您必须更改调用它的方式。
You can't get the context of the caller, but you can set the this
property on a function you call with the method applyor call. See this referencefor an explanation on this
.
您无法获取调用者的上下文,但可以this
在使用方法apply或call 调用的函数上设置属性。有关的说明,请参阅此参考资料this
。
function foo()
{
console.log( this );
}
function bar()
{
bar.prototype.func = function func()
{
foo.apply( this );
};
}
var test = new bar();
test.func();
Usually if this
is used, it's in an object oriented context. Trying to call a method of an object with another this might indicate poor design. Explain a bit more what you are trying to achieve for more applicable design patterns.
通常如果this
使用,它是在面向对象的上下文中。尝试使用另一个 this 调用对象的方法可能表明设计不佳。多解释一下您要为更适用的设计模式实现的目标。
For an example of a javascript OOP paradigm, check my answer here.