你好我有这个代码可以正常工作
var app = {
callback: null,
jqmReady: null,
pgReady: null,
// Application Constructor
initialize: function(callback) {
this.callback = callback;
this.jqmReady = $.Deferred();
this.pgReady = $.Deferred();
this.bindEvents();
},
bindEvents: function() {
document.addEventListener('deviceready', app.pgReady.resolve, false);
$(document).on("pageinit", app.jqmReady.resolve);
$.when(app.jqmReady, app.pgReady).then(app.isReady);
},
isReady: function() {
app.callback();
}
};
代码正在初始化如下:
app.initialize(function(){
navigator.notification.alert('Hello there!', function(){}, 'Notify', 'Ok');
});
但是我的isReady函数最初是这样的,并且没有调用回调函数:
isReady: function() {
this.callback();
}
为什么会这样?在this = app
函数中,isReady()
内的initialize()
的范围不是这样的吗?
有人可以向我解释为什么它不适用于this.callback()
?
答案 0 :(得分:1)
您已经创建了一个对象,而不是类或类的实例。在整个初始化函数中将this
更改为app
。您已经在isReady
和bindEvents
函数中执行了此操作。所以请继续initialize
。