You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: ch02.asciidoc
+9-9
Original file line number
Diff line number
Diff line change
@@ -155,7 +155,7 @@ The last enhancement coming to object literals is ((("object literals", "compute
155
155
156
156
==== Method Definitions
157
157
158
-
Typically, ((("object literals", "method definitions", id="ol2md")))((("method definitions", id="mdef2")))you can declare methods on an object by adding properties to it. In the next snippet, we're creating an small event emitter that supports multiple kinds of events. It comes with an `emitter#on` method ((("emitter object", id="eo2")))that can be used to register event listeners, and an `emitter#emit` method that can be used to raise events:
158
+
Typically, ((("object literals", "method definitions", id="ol2md")))((("method definitions", id="mdef2")))you can declare methods on an object by adding properties to it. In the next snippet, we're creating a small event emitter that supports multiple kinds of events. It comes with an `emitter#on` method ((("emitter object", id="eo2")))that can be used to register event listeners, and an `emitter#emit` method that can be used to raise events:
159
159
160
160
[source,javascript]
161
161
----
@@ -178,7 +178,7 @@ var emitter = {
178
178
}
179
179
----
180
180
181
-
Starting in ES6, you can declare methods on an object literal using the new method definition syntax. In this case, we can omit the colon and the `function` keyword. This is meant as a terse alternative to traditional method declarations where you need to use the `function` ((("function keyword")))keyword. The following example shows how our `emitter` object looks like when using method definitions.
181
+
Starting in ES6, you can declare methods on an object literal using the new method definition syntax. In this case, we can omit the colon and the `function` keyword. This is meant as a terse alternative to traditional method declarations where you need to use the `function` ((("function keyword")))keyword. The following example shows how our `emitter` object looks when using method definitions.
182
182
183
183
[source,javascript]
184
184
----
@@ -215,7 +215,7 @@ function name(parameters) {
215
215
}
216
216
----
217
217
218
-
You could also create ((("arrow functions", "anonymous")))((("anonymous functions")))anonymous functions, by omitting the name, when assigning the function to a variable, a property, or a function call.
218
+
You could also create ((("arrow functions", "anonymous")))((("anonymous functions")))anonymous functions, by omitting the name when assigning the function to a variable, a property, or a function call.
219
219
220
220
[source,javascript]
221
221
----
@@ -224,7 +224,7 @@ var example = function (parameters) {
224
224
}
225
225
----
226
226
227
-
Starting with ES6, you can use arrow functions as another way of writing anonymous functions. Keep in mind, there's several slightly different ways of writing them. The following piece of code shows an arrow function that's very similar to the anonymous function we just saw. The only difference seems to be the missing `function` keyword and the `=>` arrow to the right of the parameter list.
227
+
Starting with ES6, you can use arrow functions as another way of writing anonymous functions. Keep in mind, there are several slightly different ways of writing them. The following piece of code shows an arrow function that's very similar to the anonymous function we just saw. The only difference seems to be the missing `function` keyword and the `=>` arrow to the right of the parameter list.
228
228
229
229
[source,javascript]
230
230
----
@@ -382,7 +382,7 @@ throwError('this is a warning')
382
382
at throwError
383
383
----
384
384
385
-
Arrow functions are neat when it comes to defining anonymous functions that should probably be lexically bound anyway, and they can definitely make your code more terse in some situations. They are particularly useful in most functional programming situations such as when using `.map`, `.filter`, or `.reduce` on collections, as shown in the following example:
385
+
Arrow functions are neat when it comes to defining anonymous functions that should probably be lexically bound anyway, and they can definitely make your code more terse in some situations. They are particularly useful in most functional programming situations, such as when using `.map`, `.filter`, or `.reduce` on collections, as shown in the following example:
386
386
387
387
[source,javascript]
388
388
----
@@ -444,7 +444,7 @@ In a similar fashion, you could mix and match destructuring with regular variabl
444
444
var { pseudonym } = character, two = 2
445
445
----
446
446
447
-
If you want to extract a property named `pseudonym` but would like to declare it as a variable named `alias`, you can use the following destructuring syntax known as aliasing. Note that you can use `alias` or any other valid variable name:
447
+
If you want to extract a property named `pseudonym` but would like to declare it as a variable named `alias`, you can use the following destructuring syntax, known as _aliasing_. Note that you can use `alias` or any other valid variable name:
448
448
449
449
[source,javascript]
450
450
----
@@ -453,7 +453,7 @@ console.log(alias)
453
453
// <- 'Batman'
454
454
----
455
455
456
-
While aliases don't look any simpler than the ES5 flavor, `alias = character.pseudonym`, ((("aliases", id="al2")))they start making sense when you consider the fact that destructuring supports deep structures as in the following example:
456
+
While aliases don't look any simpler than the ES5 flavor, `alias = character.pseudonym`, ((("aliases", id="al2")))they start making sense when you consider the fact that destructuring supports deep structures, as in the following example:
457
457
458
458
[source,javascript]
459
459
----
@@ -469,7 +469,7 @@ var { metadata: { gender: characterGender } } = character
469
469
470
470
The scenario we just saw repeats itself frequently, because properties are often named in the context of their host object. While `palette.color.code` is perfectly descriptive, `code` on its own could mean a wide variety of things, and aliases such as `colorCode` can help you bring context back into the variable name while still using destructuring.
471
471
472
-
Whenever you access an nonexistent property in ES5 notation, you get a value of `undefined`:
472
+
Whenever you access a nonexistent property in ES5 notation, you get a value of `undefined`:
Regular ((("assignment destructuring", "function parameters", startref="ad2fpd")))((("destructuring", "function parameters", startref="d2fpd")))((("function parameters", startref="fpd2")))expressions are another great fit for destructuring. Destructuring empowers you to name groups from a match without having to resort to index numbers. Here's an example `RegExp` that could be used for parsing simple dates, and an example of destructuring those dates into each of its components. The first entry in the resulting array is reserved for the raw input string, and we can discard it.
749
+
Regular ((("assignment destructuring", "function parameters", startref="ad2fpd")))((("destructuring", "function parameters", startref="d2fpd")))((("function parameters", startref="fpd2")))expressions are another great fit for destructuring. Destructuring empowers you to name groups from a match without having to resort to index numbers. Here's an example `RegExp` that could be used for parsing simple dates, and an example of destructuring those dates into each of their components. The first entry in the resulting array is reserved for the raw input string, and we can discard it.
0 commit comments