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
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+"); |
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
function sum() { | |
var result = 0, | |
i = 0, | |
len = arguments.length; | |
while (i < len) { | |
result += arguments[i]; | |
i++; | |
} | |
return result; | |
} |
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
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 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 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 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 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 call() Method | |
The first function method for manipulating this is call(), which executes | |
the function with a particular this value and with specific parameters. | |
The first parameter of call() is the value to which this should be equal | |
when the function is executed. All subsequent parameters are the parameters that should be passed into the function. For example, suppose you | |
update sayNameForAll() to take a parameter: | |
function sayNameForAll(label) { | |
console.log(label + ":" + this.name); | |
} |
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
The apply() Method | |
The second function method you can use to manipulate this is apply(). The | |
apply() method works exactly the same as call() except that it accepts only | |
two parameters: the value for this and an array or array-like object of | |
parameters to pass to the function (that means you can use an arguments | |
object as the second parameter). So, instead of individually naming each | |
parameter using call(), you can easily pass arrays to apply() as the second | |
argument. Otherwise, call() and apply() behave identically. This example | |
shows the apply() method in action: |
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
The bind() Method | |
The third function method for changing this is bind(). This method was | |
added in ECMAScript 5, and it behaves quite differently than the other | |
two. The first argument to bind() is the this value for the new function. | |
All other arguments represent named parameters that should be permanently set in the new function. You can still pass in any parameters that | |
aren’t permanently set later. | |
The following code shows two examples that use bind(). You create | |
the sayNameForPerson1() function by binding the this value to person1, while | |
sayNameForPerson2() binds this to person2 and binds the first parameter as | |
"person2". |
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
Detecting Properties | |
Because properties can be added at any time, it’s sometimes necessary to | |
check whether a property exists in the object. New JavaScript developers | |
often incorrectly use patterns like the following to detect whether a property exists: | |
// unreliable | |
if (person1.age) { | |
// do something with age | |
} |
OlderNewer