Skip to content

Instantly share code, notes, and snippets.

@AugustoPedraza
Created March 4, 2013 13:44
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/5082325 to your computer and use it in GitHub Desktop.
Save AugustoPedraza/5082325 to your computer and use it in GitHub Desktop.
When a function is stored as a property of object, we call it a "method". When a method is invoked, "this" is bound to that object. If an invocation expression contains a refinement (that is, a "." expression or [subscript]expression), it is invoked as a method: //Show code... A method can use "this" to access to object so that it can retrieve v…
//Create myObject. It has a value and an increment method.
//The increment method takes a optional parameter.
//If the argument is not a number, then 1 is used as the default.
var myObject = {
value: 0,
increment: function(inc){
this.value +=
typeof inc === 'number' ? inc : 1;
}
};
console.log(myObject.value); //Show 0
myObject.increment(20);
console.log(myObject.value); //Show 20
myObject.increment("bad param");
console.log(myObject.value); //Show 21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment