Javascript 如何获得';这';调用函数的值?
如果我有这样一个函数:Javascript 如何获得';这';调用函数的值?,javascript,function,scope,this,Javascript,Function,Scope,This,如果我有这样一个函数: function foo(_this) { console.log(_this); } function bar() {} bar.prototype.func = function() { foo(this); } var test = new bar(); test.func(); 然后记录bar的test实例 但是,要使其工作,我必须在bar.prototype.func函数中传递this。我想知道是否有可能在不传递this的情况下获得相同的t
function foo(_this) {
console.log(_this);
}
function bar() {}
bar.prototype.func = function() {
foo(this);
}
var test = new bar();
test.func();
然后记录bar
的test
实例
但是,要使其工作,我必须在bar.prototype.func
函数中传递this
。我想知道是否有可能在不传递this
的情况下获得相同的this
值
function foo()
{
console.log( this );
}
function bar()
{
bar.prototype.func = function func()
{
foo.apply( this );
};
}
var test = new bar();
test.func();
我尝试使用arguments.callee.caller
,但这将返回原型函数本身,而不是原型函数中的this
值
通过在原型函数中只调用
foo()
,是否可以记录test
的bar
实例?我认为在bar
的上下文中调用foo
应该可以:
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'
如果问题是“未通过此项(无论如何)”,则答案是否 值可以通过其他方法传递。例如,使用全局变量(在Bar类中)或会话或cookie
function bar() {
var myThis;
function foo() {
console.log(myThis);
}
bar.prototype.func = function() {
myThis = this;
foo();
}
}
var test = new bar();
test.func();
您可以在不更改外部函数的情况下执行此操作,但必须更改调用它的方式 您无法获取调用方的上下文,但可以对使用方法或调用的函数设置
this
属性。有关此的说明,请参阅
function foo()
{
console.log( this );
}
function bar()
{
bar.prototype.func = function func()
{
foo.apply( this );
};
}
var test = new bar();
test.func();
通常,如果使用此
,则它位于面向对象的上下文中。试图用另一个对象调用对象的方法,这可能表明设计不佳。再解释一下,为了获得更适用的设计模式,您正试图实现什么
有关javascript OOP范例的示例,请检查 这个怎么样
"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();
或者这个:
"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();
谢谢,但我仍然需要传递
此
值。我的意思是,通过调用foo()
insidefunc
,我应该能够在foo
中获得这个。但是ehr,这就是这里发生的事情,还是我误解了你的问题?在我的回答中调用foo-like可以使来自实例的test
对foo可用,这可以通过log
记录它的testVal属性来证明。我很抱歉没有弄清楚。我的意思是在foo
中获得this
的func
而不是在func
中实际传递this
。好吧,我真的不明白它的用途,但是嘿。实际上,foo.call(this)
不会传递this
(如您所见,foo
没有参数,没有传递任何内容),而是将foo
的上下文设置为this
,它是由bar
构造函数创建的实例,或者您无法访问调用堆栈中函数的上下文。您必须传递此上下文。如果您已经准备创建一个特殊用途的函数来记录附加到bar原型的日志,您是否有理由不传递此?使用这种模式除了出于学习目的之外还有其他原因吗?@TNi:它不一定有实际目的,但在我的场景中会很有用。我试图通过将函数和边界传递给函数来实现多级for循环,函数将像for循环一样执行函数。在常规for循环中,可以访问this
,但通过这种方式,我似乎需要将this
传递给我的函数。我认为您最初的方法是正确的。最佳实践是将对象实例传递给全局函数。是的,它可能有实际用途。有时,您不选择JS如何调用函数(回调、内部结构等)。您仍然在传递这个。我发现foo.apply(this)
要干净得多/不那么刻薄。我不是在传递它,而是将值设置为一个变量,该变量可以在Bar类的命名空间内访问。这是一个好的、正确的方法。@Raynos:没错,但我接受了,因为我声明这是不可能的。这基本上回答了我的问题。@DmitriyNaurnov您没有通过函数参数传递它,而是通过bar局部范围传递它,这是一个hack@Raynos例如我不同意。我建议你阅读维基百科关于所谓黑客的内容;)主题已关闭。