DI Inheriter

by Sam Fereday

HTML

<div id="output"></div>

JavaScript

var output = document.getElementById("output");

// Make a parent class
var Parent = function(someParentArgument){
    this.someParentArgument = someParentArgument || {};
}

Parent.prototype = {

    parentProperty: 1000,
    parentFunc: function(){
        return this.parentProperty;
    },
    overrideProperty: 100

}

// Make a child class
var Child = function(someArgument){
    this.someArgument = someArgument || {};
};

Child.prototype = {
    
    childProperty: 69,
    childFunc: function(){
       return this.childProperty;
    },
    overrideProperty: 3
    
}

// Make the child extend the parent
function extend(C, P, args) {

    var protoProps = C.prototype;
    C.prototype = Object.create(P.prototype);
    C.prototype.constructor = C;
    //P.apply(C, args);
    
    for(var key in protoProps) {
       C.prototype[key] = protoProps[key];
    }
    
}

// Run extend
extend(Child, Parent);

// Doesn't work, probably because the prototype gets overwritten by the parent...
var newthing = new Child();
console.log(newthing);
console.log(newthing.overrideProperty);
console.log(newthing.parentProperty);