Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created September 7, 2012 09:54
Show Gist options
  • Save davidaurelio/3664757 to your computer and use it in GitHub Desktop.
Save davidaurelio/3664757 to your computer and use it in GitHub Desktop.
Naive implementation of super in JS
Object.defineProperty(Object.prototype, 'super', {
get: function() { return Object.getPrototypeOf(this); }
});
function Foo() {}
Foo.prototype.reset = function() {
this.fooProp = 'reset';
console.log('Foo#reset()');
}
function Bar() {}
Bar.prototype = new Foo();
Bar.prototype.reset = function() {
this.barProp = 'reset';
console.log('Bar#reset()');
this.super.reset();
}
var b = new Bar();
b.reset();
JSON.stringify(b); // '{"barProp":"reset"}'
JSON.stringify(Bar.prototype); // '{"barProp":"reset"}'
JSON.stringify(Foo.prototype); // '{"fooProp":"reset"}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment