I'm new to Javascript, so could anyone tell me why the value of 'this' changes depending on which function I'm calling it from. Am I not calling the addquote
as a method (#3 from here? I mostly wrote this as an OOP exercise, so if anyone could point me in the way of good documentation that would be very much appreciated. Thanks!
function QuoteBox(text, author){
this.quote = new Quote(text, author);
this.element = document.getElementById("quotebox");
}
QuoteBox.prototype.removequote = function(c){
// do some animation here and maybe move the removal to a callback
this.element.children[0].innerHTML = "";
this.element.children[1].innerHTML = "";
c();
}
QuoteBox.prototype.addquote = function(){
console.log(this); // outputs the window ?!
this.element.children[0].innerHTML =quote.text;
this.element.children[1].innerHTML =quote.author;
}
QuoteBox.prototype.refresh = function(){
console.log(this); // outputs the quotebox object
this.quote.generate();
this.removequote(this.addquote);
}
$(document).ready(function() {
quotebox = new QuoteBox("Quote", "Author");
window.setInterval(function(){
quotebox.refresh();
}, 5000);
});