Created
July 23, 2011 17:30
-
-
Save abernier/1101653 to your computer and use it in GitHub Desktop.
Method/Function invocation pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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