Enables a semblance of classical inheritance. - Node.js Object

Node.js examples for Object:Inheritance

Description

Enables a semblance of classical inheritance.

Demo Code

 
/**/*w ww.ja  v a 2 s.c o m*/
 * Enables a semblance of classical inheritance.
 * @see http://backbonejs.org/docs/backbone.html#section-208
 *
 * @param protoProps
 * @param staticProps
 * @returns {*}
 */
var extend = function(protoProps, staticProps) {
    var parent = this;
    var child, has = Object.prototype.hasOwnProperty;
        if (protoProps && has.call(protoProps, 'constructor')) {
        child = protoProps.constructor;
    }
        else {
        child = function(){ return parent.apply(this, arguments); };
    }
    if(staticProps)
    for(var static_prop in staticProps){
        child[static_prop] = staticProps[static_prop];
    }
    var Surrogate = function(){ this.constructor = child; };
    Surrogate.prototype = parent.prototype;
    child.prototype = new Surrogate;

    if (protoProps)
    for(var proto_prop in protoProps){
        child.prototype[proto_prop] = protoProps[proto_prop];
    }
    child.extend = extend;
    child.__super__ = parent.prototype;

    return child;
};

Related Tutorials