Skip to content

Instantly share code, notes, and snippets.

@ysmood
Last active February 24, 2016 07:54
Show Gist options
  • Save ysmood/fb278961783a60221103 to your computer and use it in GitHub Desktop.
Save ysmood/fb278961783a60221103 to your computer and use it in GitHub Desktop.
Subclass Example
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