Created
March 4, 2013 13:44
-
-
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…
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
//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