Created
August 10, 2015 14:06
-
-
Save developit/9b975fec11f26276182f to your computer and use it in GitHub Desktop.
bind-decorator.js
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
/** Bind a method to lexical scope. | |
* @example | |
* class Foo { | |
* @bind | |
* bar() { | |
* assert.ok( this instanceof Foo ); | |
* } | |
* } | |
*/ | |
export default (target, key, { value: fn }) => ({ | |
configurable: true, | |
get() { | |
let value = fn.bind(this); | |
Object.defineProperty(this, key, { | |
value, | |
configurable: true, | |
writable: true | |
}); | |
return value; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment