I am studying prototypes, classes and modular patterns with Alex MacCaw's Javascript Web Applications. Almost everything is clear and well-explained, however, I can't figure out how empty functions that are used for initialization work. I would be grateful if your explain nuances no matter how complex they are.
Here is the example from the book:
(function($){
var mod = {};
mod.create = function(includes){
var result = function(){
this.init.apply(this, arguments);
};
result.fn = result.prototype;
result.fn.init = function(){};
result.proxy = function(func){ return $.proxy(func, this); };
result.fn.proxy = result.proxy;
result.include = function(ob){ $.extend(this.fn, ob); };
result.extend = function(ob){ $.extend(this, ob); };
if (includes) result.include(includes)
return result;
};
exports.Controller = mod;
})(jQuery);
From the code above I understand that the Immediately Invoked Function Expression (IIFE) is used to protect the scope. Then mod.create construction function is defined that returns result object with all class methods. However, I am confused with how the following works:
this.init.apply(this, arguments);
result.fn.init = function(){};
I guess that we apply empty init function to constructor arguments to allow new objects instantiation or something like that. From the answer below it appears that init function receives undefined number of arguments, but what arguments? Those that are used during instantiation -- includes in the above code? And why this function is empty, what it does when invoked?