`super` using `Proxy.create`
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
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