Recently, I've been working on a project with angularjs and phonegap and stumbled upon this interesting code snippet. While I have a basic understanding of what it does, the inner workings are still a bit unclear to me. Since I'm still getting familiar with advanced javascript concepts, I would greatly appreciate if someone could provide a more detailed explanation.
Here are my questions:
Within the callback registration, I noticed that the variables "arguments" (line 5) and "fn" (line 10) are not explicitly defined anywhere in the function. Are they declared within the function prototype?
Regarding the code blocks on lines 9 and 15, are they simply setting the value of "this" so that it points to the same object within the callback? What is the term used for this technique of applying the "this" value?
myApp.factory('phonegapReady', function() {
return function (fn) {
var queue = [];
var impl = function () {
queue.push(Array.prototype.slice.call(arguments));
};
document.addEventListener('deviceready', function () {
queue.forEach(function (args) {
fn.apply(this, args);
});
impl = fn;
}, false);
return function () {
return impl.apply(this, arguments);
};
};
});