Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.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_Oop_Scope - Fatal编程技术网

在侦听器函数(Javascript)内调用方法时发生引用错误

在侦听器函数(Javascript)内调用方法时发生引用错误,javascript,oop,scope,Javascript,Oop,Scope,当我点击按钮a时,我得到一个引用错误,因为refreshFields方法没有“找到” 未捕获引用错误:未在处定义refreshFields file:///android_asset/www/src/pages/main.js:70 但如果我在其他地方调用该方法,而不是在那个tap侦听器中,它就可以工作 我完全确定,tap listener函数中的this引用的是按钮a,即事件目标 我的问题是:什么是最好的(oo)修复方法?试试这个 function LolClass(){ this.

当我点击按钮a时,我得到一个引用错误,因为refreshFields方法没有“找到”

未捕获引用错误:未在处定义refreshFields file:///android_asset/www/src/pages/main.js:70

但如果我在其他地方调用该方法,而不是在那个tap侦听器中,它就可以工作

我完全确定,tap listener函数中的
this
引用的是按钮a,即事件目标

我的问题是:什么是最好的(oo)修复方法?

试试这个

function LolClass(){

    this.init = function(){             
        button_a.bind("tap", function(){                
            this.refreshFields(); // doesn't work 
            //refreshFields(); // doesn't work either
        });     
    }

    this.refreshFields = function(){
        alert("LOL");
    }

    this.dummy = function(){
        this.refreshFields(); // W O R K S!
    }
}

您应该缓存此:

function LolClass(){

    var someVar = 0; 
    var self = this;

    this.init = function(){             
        button_a.bind("tap", function(){                
            self.refreshFields(); // now works!
            //refreshFields(); // doesn't work
        });     
    }

    this.refreshFields = function(){
        alert("LOL");
    }

    this.dummy = function(){
        this.refreshFields(); // W O R K S!
    }
}

您需要修改代码:

var that = this; // "that" is a convention name for "this"
this.init = function(){             
        button_a.bind("tap", function(){                
            that.refreshFields(); 
        });     
    }

回调中的“this”指的是不同的对象。我添加了var$this=this;并在回调中使用$this。

当你复制他的代码并修复它时,你可以删除“不工作”<代码>:)由于所有答案都正确,我将接受第一次提交的答案。谢谢当你复制他的代码并修复它时,你可以删除“不工作”
:)
的坏名字,this
通常指
$(this)
。将其更改为
that
self
。我认为
指的是
按钮a
,因此它不在正确的范围内。我不完全确定,但您可能必须将父对象传递到函数中才能在函数中引用它。什么是
.bind()
?它是图书馆的一部分吗?如果是这样,那么
.bind()
方法是否接受
上下文
参数?请注意,javascript中的此模式已被弃用。如果忘记了
new
操作符,可能会发生非常糟糕的事情。你应该使用
closure
@gdoron,你能给我提供与那次弃用有关的链接吗?@MarceloAssis。一本书——我不知道那个惯例。我在哪里可以找到更多关于它的信息?@MarceloAssis。要知道的太多了,它只是缓存的
this
的一个名称。
function LolClass(){

    var someVar = 0; 
    var $this = this;

    this.init = function(){             
        button_a.bind("tap", function(){                
            $this.refreshFields();
        });     
    }

    this.refreshFields = function(){
        alert("LOL");
    }

    this.dummy = function(){
        this.refreshFields(); // W O R K S!
    }
}