Return to Snippet

Revision: 61628
at December 29, 2012 08:52 by codernator


Initial Code
/*jslint plusplus: true, vars: true, browser: true, devel: false, maxerr: 5, maxlen: 140 */
function ObjectBoilerPlateModule() {
    "use strict";

    // addMethod - By John Resig (MIT Licensed)
    // add a method to an object.
    // creates function overloading scenario based entirely on length of argument list.
    // won't work to overload functions according to argument "type".
    function addMethod(object, name, fn) {
        var old = object[name];
        object[name] = function () {
            if (fn.length === arguments.length) {
                return fn.apply(this, arguments);
            }

            if (typeof old === 'function') {
                return old.apply(this, arguments);
            }

            throw "Invalid";
        };
    }

    function inherit(parentPrototype) {
        function F() {}
        F.prototype = parentPrototype;
        return new F();
    }


    this.addMethod = addMethod;
    this.inherit = inherit;
}

Initial URL


Initial Description
# addMethod - By John Resig (MIT Licensed)
Add a method to an object. Creates function overloading scenario based entirely on length of argument list. Won't work to overload functions according to argument "type".

# inherit
Create parameter-less inheritance chain for situations in which the parent object takes arguments in its "constructor".

Usage:

    function A(a, b) { }
    function B() { A.call(this, 1, 2); }
    B.prototype = inherit(A.prototype);

Initial Title
ObjectBoilerPlateModule - some methods to assist creating objects

Initial Tags


Initial Language
JavaScript