Skip to content

Instantly share code, notes, and snippets.

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 AugustoPedraza/5082449 to your computer and use it in GitHub Desktop.
Save AugustoPedraza/5082449 to your computer and use it in GitHub Desktop.
var sum = add(3, 4); //sum is 7 When a function is invoked with this pattern, "this" is bound to the global object. If the methods defines a variable and assigns it the value of "this", the inner function will have access to "this" through that variable. By convention, the name of that variable is "that". This text and example was taken from the…
//Augment myObject with a double method.
add = function(a, b){ return a + b; };
var myObject = { value: 0 };
myObject.double = function( ){
var that = this; //Workaround
var helper = function( ){ that.value = add(that.value, that.value); };
helper( ); //Invoke helper as a function
};
console.log(myObject.value); //Show 0
myObject.value = 2;
myObject.double( );
console.log(myObject.value); //Show 4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment