Skip to content

Instantly share code, notes, and snippets.

@davidaurelio
Created September 7, 2012 10:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidaurelio/3664889 to your computer and use it in GitHub Desktop.
Save davidaurelio/3664889 to your computer and use it in GitHub Desktop.
`super` using `Proxy.create`
Object.defineProperty(Object.prototype, 'super', {
value: function(Constructor) {
var context = this;
var superProto = Object.getPrototypeOf(Constructor.prototype);
return Proxy.create({
get: function(_, name) {
return function() {
return superProto[name].apply(context, arguments);
}
}
});
}
});
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(Bar).reset();
}
var b = new Bar();
b.reset();
JSON.stringify(b); // '{"barProp":"reset","fooProp":"reset"}'
JSON.stringify(Bar.prototype); // '{}'
JSON.stringify(Foo.prototype); // '{}'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment