Skip to content

Instantly share code, notes, and snippets.

@abernier
Created July 23, 2011 17:30
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 abernier/1101653 to your computer and use it in GitHub Desktop.
Save abernier/1101653 to your computer and use it in GitHub Desktop.
Method/Function invocation pattern
var o = {
value: 3
};
* Method invocation
When invoking a function that is a property of an object
o.incr = function () {
this.value += 1; // 'this' will be bound to 'o'
};
o.incr(); // Method invocation pattern
* Function invocation
When invoking a function that is not a property of an object
o.double = function () {
var that = this; // save the context! (Workaround)
function helper() {
//this.value *= 2;
that.value *= 2;
};
helper(); // Function invocation pattern: 'this' will be bound to the global object
};
o.double();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment