I'm trying to use the Revealing Module pattern together with the Constructor pattern to create somewhat beautiful JS! I want to be able to create multiple instances if "Countdown" like this:
var c1 = new Countdown();
c1.init();
var c2 = new Countdown();
c2.init();
These should be independent. So instead of declaring variables with "var", I use "this" on the prototype. I reference "this" inside a function, but it's undefined. How can I access "this"?
var Countdown = function() {};
Countdown.prototype = (function(doc) {
return {
init: init
};
function init() {
// day
this.d1 = doc.createElement('div');
this.d1.setAttribute('class', 'day-1 elem');
// ... more elements left out here
}
function updateView(time) {
this.d1.textContent = getDays(secs)[0];
}
function getDays(secs) {
var result = 0;
if (secs > 0) {
result = secs / (60 * 60 * 24);
}
return Math.floor(result);
}
})(document);
Aucun commentaire:
Enregistrer un commentaire