Skip to content

Instantly share code, notes, and snippets.

@airbr
Forked from jashmenn/self-eq-this-vs-bind.md
Created February 16, 2017 20:21
Show Gist options
  • Select an option

  • Save airbr/7a1b0c953e93146a67bfeea01e4b7e10 to your computer and use it in GitHub Desktop.

Select an option

Save airbr/7a1b0c953e93146a67bfeea01e4b7e10 to your computer and use it in GitHub Desktop.
Javascript var self = this; vs. .bind

In Javascript this is bound to the current function so when you want to keep a reference to an "outer" object you see the pattern: var self = this;

as in:

var self = this;
return function(whatever) {
  return self.foo(whatever); // `this` here is for the inner function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment