1

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?

1 Answer 1

1
this.init.apply(this, arguments);

is basically just

this.init(arguments[0], arguments[1], ..., arguments[N]);

But as soon as we don't know the exact number of arguments - we just use .apply()

Usage example:

var newConstructor = exports.Controller.create();
newConstructor.include({
    init: function(a, b, c) { console.log(a, b, c, 'yay!'); }
});

var newInstance = new newConstructor(1, 2, 3);

Complete demo: http://jsfiddle.net/nbq6d/1/

Sign up to request clarification or add additional context in comments.

7 Comments

Ok, I understand that it works on arguments array.. So it 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?
@user3695711: they are the arguments passed to a result constructor
Thanks, but still I would like to understand inner workings of this concept. So init function receives argument passed to result constructor. We get init(arg1,arg2....). How it processes them if it's empty - init: function(){}?
@user3695711: added sample example on how it could be used according the code you provided
@Grundy: in this case it's likely an empty function was used as a default implementation that might be overriden using .include()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.