I'm experiencing difficulty figuring out what 'this' refers to, in multiple instances of the following code:
jQuery(function ($) {
var coreChat = {
fetch: function (fn) {
$.ajax({
url: "https://api.parse.com/1/classes/chats",
data: {
order: 'createdAt',
limit: 10
},
type: "GET"
})
.done(function(data) {
var messages = [];
for (var i = 0, len = data.results.length; i < len; i++) {
messages.push(data.results[i].text);
}
return fn(messages);
});
},
send: function (message) {
var data = JSON.stringify({text: message});
$.ajax({
url: "https://api.parse.com/1/classes/chats",
data: data,
type: "POST"
});
},
display: function (messages) {
// http://jsperf.com/update-only-text-vs-remove-dom
var messageNode = $(".messages").find("li");
messageNode.each(function (i) {
var $this = $(this);
if ($this.text() !== messages[i]) {
$this.text(messages[i]);
}
});
}
};
var myChat = {
init: function () {
**this**.fetchInitialData();
**this**.bindSendEvent();
**this**.refresh();
},
fetchInitialData: function () {
var messagesWrapper = $(".messages");
for (var i = 0; i < 10; i++) {
$("<li></li>").appendTo(messagesWrapper);
}
myChat.updateMessages();
},
bindSendEvent: function () {
$(".send").on("click", function (e) {
var input = $(".draft");
myChat.send(Chat.username + ": " + input.val());
input.val("");
e.preventDefault();
});
},
refresh: function () {
setInterval(function () {
myChat.updateMessages();
}, 3000);
},
updateMessages: function () {
this.fetch(function (messages) {
myChat.display(messages);
});
}
};
$.extend(myChat, coreChat);
myChat.init();
});
More specifically, in the bottom half of the code - in the myChat object, there is an "init" property which, as a value, contains a function...
var myChat = {
init: function () {
this.fetchInitialData();
this.bindSendEvent();
this.refresh();
},
What would 'this' be referring to here? And does the fact that coreChat was extended into myChat on the 2nd to last line of the code make a difference in determining what 'this' refers to?
$.extend(myChat, coreChat);
Also, there's another 'this' in the updateMessages property of the myChat object...
updateMessages: function () {
this.fetch(function (messages) {
myChat.display(messages);
});
}
^ What does this refer to in this instance? The fetch function from coreChat is being executed on 'this', but again, what is 'this'?
And as a quick tangential question - in the updateMessages function, it calls the 'fetch' function.'fetch' is asynchronous with a callback function ('fetch' is originally found in the coreChat object). But then in updateMessages, what is that function that is passed in as a parameter to fetch? Is fetch supposed to execute it's original callback function, and then this parameter is to act as the parameter of the callback? I'm definitely confused there.
^ I understand that's 2 separate questions - I'll delete it if that poses an issue, and post it in a new separate question, but I just thought, since someone may have already gone thru the entire code, maybe he'd/she'd already be equipped to answer the 2nd question and I might as well ask...
THANKS IN Advance!