Skip to content

Instantly share code, notes, and snippets.

@developit
Created August 10, 2015 14:06
Show Gist options
  • Save developit/9b975fec11f26276182f to your computer and use it in GitHub Desktop.
Save developit/9b975fec11f26276182f to your computer and use it in GitHub Desktop.
bind-decorator.js
/** 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