Monday, February 20, 2017

Understanding Javascript Prototype Inheritence

    MyBaseObj = function(a) {
        this.a = a
    }
    MyObjBase.prototype.aSquared = function() {
        return a ^ 2
    }
    MyObj1 = function(a, b) {
        MyBaseObj.call(this, a)
        this.b = b
    }
    MyObj1.prototype = Object.create(MyBaseObj.prototype)
    MyObj1.prototype.constructor = MyObj1
    MyObj2 = function(a, c) {
        MyBaseObj.call(this, a)
        this.c = c
    }
    MyObj2.prototype = Object.create(MyBaseObj.prototype)
    MyObj2.prototype.constructor = MyObj2

No comments: