Last active
February 24, 2016 07:54
-
-
Save ysmood/fb278961783a60221103 to your computer and use it in GitHub Desktop.
Subclass Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function A () { | |
this._v = 1; | |
this.bar = function () { | |
return this._v; | |
} | |
} | |
A.prototype.foo = function () { | |
return this._v; | |
}; | |
function B () { | |
var internalProto = Object.getPrototypeOf(this); | |
var parentInternalProto = Object.getPrototypeOf(internalProto); | |
this.parentInternalProto = new (parentInternalProto.constructor); | |
Object.setPrototypeOf(internalProto, this.parentInternalProto); | |
} | |
Object.setPrototypeOf(B.prototype, A.prototype); | |
B.prototype.foo = function () { | |
return this.parentInternalProto.foo() + this._v; | |
}; | |
B.prototype.bar = function () { | |
return this.parentInternalProto.bar() + this._v; | |
}; | |
a = new A() | |
b = new B() | |
console.log(a.foo(), b.foo()) | |
console.log(a.bar(), b.bar()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment