I recently inherited a large JavaScript file from a previous developer, and I'm trying to decipher some of the key sections. Here is the complete code:
$(function () {
var homepage = (function () {
// Main functionalities are defined here...
return {
init: function () {
// This section initializes various functions
}
}
})();
homepage.init();
});
One specific aspect that I'd like clarity on revolves around the following segment:
var homepage = (function () {
...
return {
init: function () {
// Initiating certain functions here
}
}
})();
homepage.init();
My questions are:
1) What are the benefits of creating the homepage
function in this particular manner?
2) I am also puzzled by how the function returns an object containing another JavaScript function init
, which is later invoked using homepage.init();
. Does calling homepage.init();
simultaneously execute both the homepage
and init
functions?
Thank you for your assistance!