Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何获得';这';调用函数的值?_Javascript_Function_Scope_This - Fatal编程技术网

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()
inside
func
,我应该能够在
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例如我不同意。我建议你阅读维基百科关于所谓黑客的内容;)主题已关闭。