This file contains hidden or 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
| The this Object | |
| You may have noticed something strange in the previous example. The | |
| sayName() method references person.name directly, which creates tight coupling between the method and the object. | |
| This is problematic for a number of reasons. First, if you change the variable name, you also need to | |
| remember to change the reference to that name in the method. Second, | |
| this sort of tight coupling makes it difficult to use the same function for | |
| different objects. Fortunately, JavaScript has a way around this issue. | |
| Every scope in JavaScript has a this object that represents the calling object for the function. | |
| In the global scope, this represents the | |
| global object (window in web browsers). When a function is called while |
This file contains hidden or 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
| Object Methods | |
| As mentioned in Chapter 1, you can add and remove properties from | |
| objects at any time. When a property value is actually a function, the | |
| property is considered a method. You can add a method to an object in | |
| the same way that you would add a property. For example, in the following code, the person variable is assigned an object literal with a name property and a method called sayName. | |
| var person = { | |
| name: "Nicholas", | |
| sayName: function() { | |
| console.log(person.name); |
This file contains hidden or 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
| Object Methods | |
| As mentioned in Chapter 1, you can add and remove properties from | |
| objects at any time. When a property value is actually a function, the | |
| property is considered a method. You can add a method to an object in | |
| the same way that you would add a property. For example, in the following code, the person variable is assigned an object literal with a name property and a method called sayName. | |
| var person = { | |
| name: "Nicholas", | |
| sayName: function() { | |
| console.log(person.name); |
This file contains hidden or 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
| overloading | |
| Most object-oriented languages support function overloading, which is the | |
| ability of a single function to have multiple signatures. A function signature | |
| is made up of the function name plus the number and type of parameters | |
| the function expects. Thus, a single function can have one signature that | |
| accepts a single string argument and another that accepts two numeric | |
| arguments. The language determines which version of a function to call | |
| based on the arguments that are passed in. | |
| As mentioned previously, JavaScript functions can accept any number | |
| of parameters, and the types of parameters a function takes aren’t speci- |
This file contains hidden or 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
| function sum() { | |
| var result = 0, | |
| i = 0, | |
| len = arguments.length; | |
| while (i < len) { | |
| result += arguments[i]; | |
| i++; | |
| } | |
| return result; | |
| } |
This file contains hidden or 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
| const items = new Array(); | |
| const now = new Date(); | |
| const error = new Error("Something bad happened."); | |
| const func = new Function("console.log('Hi');"); | |
| const object = new Object(); | |
| const re = new RegExp("\\d+"); |
NewerOlder