我有这个对象:
var crudConfig = function($wizard, $formModal, $deleteModal) {
'use strict';
return {
handleOnShowFormModal : function() {
$formModal.on('show.bs.modal', function(event) {
...................
this.fillForms(data);
....................
});
return this;
},
fillForms : function(data) {
//do stuff
return this;
}
}
}
当我使用参数调用 fillForms 时,出现问题。
Uncaught TypeError: this.fillForms is not a function
由于 fillForms 键是一个匿名函数,我如何从对象内部调用它?在其他相关问题上,我只发现如果键具有字符串值并且我像这样调用: this.fillForms
。
最佳答案
回调中的
this
引用 $formModal
元素。您需要做的是在调用事件监听器之前将引用该对象的 this
存储在变量中,并在回调中使用该变量来访问该对象。
就像这样:
handleOnShowFormModal : function() {
var _this = this
$formModal.on('show.bs.modal', function(event) {
_this.fillForms(data);
});
return this;
},
关于javascript - 在对象中引用自身匿名函数键Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37901542/