これは私のJavaScriptコードです。その目標は教育のみです。私はjsOOPとjqueryを勉強しています
function App() {
this.deviceReadyDeferred = new $.Deferred();
this.init = function() {
console.log ("Run");
$.when(this.deviceReadyDeferred).then(this.run);
document.addEventListener("click", this.onDeviceReady, false);
},
// NB: onDeviceReady non ha parametri evento
this.onDeviceReady = function() {
console.log("deviceReady");
this.deviceReadyDeferred.resolve();
},
this.run = function() {
console.log("main");
}
}
app = new App();
app.init();
クリックすると、
TypeError:this.deviceReadyDeferredは未定義です
なんで?
- '$'が未定義であるため、jQueryは正常に実行されています。
- Win7のFF19.0.2でjQuery1.9.1を実行しています
javascriptオブジェクトにdeferedを使用するにはどうすればよいですか?それを初期化して再利用する方法は?
編集:
このコードは機能しています。すべての問題は私の誤用にありthis
ました。私はJavaScriptを使ったOOPの初心者です。
function App() {
var self = this;
this.deviceReadyDeferred = new $.Deferred();
this.init = function() {
console.log ("Run");
$.when(self.deviceReadyDeferred).then(self.run);
$(document).on("click", self.onClick);
},
this.onClick = function() {
console.log("deviceReady");
self.deviceReadyDeferred.resolve();
},
this.run = function() {
console.log("main");
}
}
app = new App();
app.init();