Skip to content

Instantly share code, notes, and snippets.

@yangsu
Created January 29, 2013 21:09
Show Gist options
  • Save yangsu/4667889 to your computer and use it in GitHub Desktop.
Save yangsu/4667889 to your computer and use it in GitHub Desktop.
JavaScript Function Invocation - Function Invotation
// this refers to the global object (a mistake in the language design)
obj.quadruple = function () {
var that = this; // common idiomatic workaround for preserving context
var helper1 = function () {
that.value = that.value * 2;
};
var helper2 = function () {
this.value = this.value * 2;
};
helper1();
helper2.call(this); // Alternatively, bind this when invoking
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment