在我的主干 View 中,我有一个函数调用同一 View 中定义的另一个函数,如下所示:
View.FormCustomer = CommonViews.FormCustomer.extend({
title : "Ajouter une fiche client",
events : {
"keypress .customerForm" : "getKeyCodeD"
},
getKeyCode : function(e) {
if (e.keyCode == "13") {
this.save(e);
}
},
getKeyCodeD : function(e) {
$(document).on('keypress.namespace', function(e) {
this.getKeyCode(e);
});
},
当我通过按键调用 getKeyCodeD 函数时,我收到一条错误,显示 Uncaught TypeError: this.getKeyCode is not a function
我认为 $(document).on
是问题所在,因为当我直接调用 getKeyCode 函数时,调用 this.save(e);
没有问题
如何在初始化时不使用 $(document).on
来解决这个问题?
最佳答案
问题在于范围变更。 this
关键字在执行期间指向 #document
,因此您应该重新绑定(bind)您想要的范围 - 主干类:
$(document).on('keypress.namespace', function(e) {
this.getKeyCode(e);
}).bind( this );
这里是workaround .
关于javascript - 未捕获的 TypeError : this. getKeyCode 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35312339/