Created
November 14, 2016 06:41
-
-
Save sairion/69f7e0e9da58a9995e6a2e7e0d1162cc to your computer and use it in GitHub Desktop.
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
{// scoped | |
class A { | |
a () { return 1 } | |
} | |
// wraps existing function | |
function baseWrapApplier(theConstructor, appliyingKey, descriptor) { | |
if (theConstructor.prototype[appliyingKey]) { | |
} | |
return function WrapApplierConstructor(constructorArgs) { | |
const obj = Object.create(theConstructor.prototype) | |
return Object.defineProperty(obj, appliyingKey, descriptor) | |
} | |
} | |
function wrapApplier(theConstructor, appliyingKey, value) { | |
return baseWrapApplier(theConstructor, appliyingKey, { value }) | |
} | |
const A2 = wrapApplier(A, 'a', function a(){ return 2; }) | |
const a = new A2 | |
console.log(a.a()) | |
console.assert(a.a() === 2, 'Failed: value is', a.a()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment