Skip to content

Instantly share code, notes, and snippets.

@dhrrgn
Created October 30, 2016 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhrrgn/3595754600c3587b221d731985e7b1ae to your computer and use it in GitHub Desktop.
Save dhrrgn/3595754600c3587b221d731985e7b1ae to your computer and use it in GitHub Desktop.
// Bind in constructor
class BindInConstructor {
constructor() {
this.foo = this.foo.bind(this);
this.bar = this.bar.bind(this);
}
foo() {
this.bar();
}
bar() {
console.log('Hello');
}
}
// Bind at call time
class BindAtCallTime {
foo() {
this.bar.call(this);
}
bar() {
console.log('Hello');
}
}
// Use class properties and arrow functions
class ClassPropertiesArrowFunctions {
foo = () => {
this.bar();
};
bar = () => {
console.log('Hello');
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment