Created
May 5, 2018 06:10
-
-
Save ArcanoxDragon/d6fe3ecba7b76d9444695727748e0621 to your computer and use it in GitHub Desktop.
React method binding decorator
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
export function BindMethod( target: any, propertyName: string, descriptor: PropertyDescriptor ) { | |
let unboundFunc = descriptor.value; | |
return { | |
configurable: true, | |
get() { | |
let boundFunc = unboundFunc.bind( this ); | |
// Define instance property so we don't have to re-bind it every time the function is accessed | |
Reflect.defineProperty( this, propertyName, { | |
value: boundFunc, | |
configurable: true, | |
writable: true | |
} ); | |
return boundFunc; | |
} | |
} as PropertyDescriptor; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment