This is my javascript code Its goal is only for education. I'm studying js OOP and 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();
When I click, I receive
TypeError: this.deviceReadyDeferred is undefined
Why?
- I don't receive a '$' is undefined, so jQuery is running fine.
- I'm running jQuery 1.9.1 on FF 19.0.2 on Win 7
How to use deferred into a javascript object? How to init and reuse it ?
EDIT:
this code is working.
All the problem was in my misuse of this
. I'm newbie at OOP with javascript.
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();