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
+36-37
Original file line number
Diff line number
Diff line change
@@ -401,70 +401,69 @@ This ((("arrow functions", startref="af2")))((("arrow functions", "merits and us
401
401
402
402
==== Destructuring Objects
403
403
404
-
Imagine ((("assignment destructuring", "objects", id="ad2o")))((("destructuring", "objects", id="d2o")))((("objects", "destructuring", id="o2d")))you had a program with some comic book characters, Bruce Wayne being one of them, and you want to refer to properties in the object that describes him. Here's the example object we'll be using for Batman:
404
+
Imagine ((("assignment destructuring", "objects", id="ad2o")))((("destructuring", "objects", id="d2o")))((("objects", "destructuring", id="o2d")))you had a program describing a color palette and you want to refer to properties in the object that describes it. Here's an example object we'll be using for our palette:
If you wanted a `pseudonym` variable referencing `character.pseudonym`, you could write the following bit of ES5 code. This is commonplace when, for instance, you'll be referencing `pseudonym` in several places in your codebase and you'd prefer to avoid typing out `character.pseudonym` each time:
418
+
If you wanted a `profile` variable referencing `palette.profile`, you could write the following bit of ES5 code. This is commonplace when, for instance, you'll be referencing `profile` in several places in your codebase and you'd prefer to avoid typing out `palette.profile` each time:
420
419
421
420
[source,javascript]
422
421
----
423
-
var pseudonym = character.pseudonym
422
+
var profile = palette.profile
424
423
----
425
424
426
-
With destructuring in assignment, the syntax becomes a bit more clear. As you can see in the next example, you don't have to write `pseudonym` twice, while still clearly conveying intent. The following statement is equivalent to the previous one written in ES5 code:
425
+
With destructuring in assignment, the syntax becomes a bit more clear. As you can see in the next example, you don't have to write `profile` twice, while still clearly conveying intent. The following statement is equivalent to the previous one written in ES5 code:
427
426
428
427
[source,javascript]
429
428
----
430
-
var { pseudonym } = character
429
+
var { profile } = palette
431
430
----
432
431
433
432
Just like you could declare multiple comma-separated variables with a single `var` statement, you can also declare multiple variables within the curly braces of a destructuring expression:
434
433
435
434
[source,javascript]
436
435
----
437
-
var { pseudonym, name } = character
436
+
var { profile, name } = palette
438
437
----
439
438
440
439
In a similar fashion, you could mix and match destructuring with regular variable declarations in the same `var` statement. While this might look a bit confusing at first, it'll be up to any JavaScript coding style guides you follow to determine whether it's appropriate to declare several variables in a single statement. In any case, it goes to show the flexibility offered by destructuring syntax:
441
440
442
441
[source,javascript]
443
442
----
444
-
var { pseudonym } = character, two = 2
443
+
var { profile } = palette, two = 2
445
444
----
446
445
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:
446
+
If you want to extract a property named `profile` but would like to declare it as a variable named `id`, you can use the following destructuring syntax, known as _aliasing_. Note that you can use `id` or any other valid variable name:
448
447
449
448
[source,javascript]
450
449
----
451
-
var { pseudonym: alias } = character
452
-
console.log(alias)
453
-
// <- 'Batman'
450
+
var { profile: id } = palette
451
+
console.log(id)
452
+
// <- 'intense-red'
454
453
----
455
454
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:
455
+
While aliases don't look any simpler than the ES5 flavor, `id = palette.profile`, ((("aliases", id="al2")))they start making sense when you consider the fact that destructuring supports deep structures, as in the following example:
457
456
458
457
[source,javascript]
459
458
----
460
-
var { metadata: { gender } } = character
459
+
var { color: { code } } = palette
461
460
----
462
461
463
462
In cases like the previous one, where you have deeply nested properties being destructured, you might be able to convey a property name more clearly if you choose an alias. Consider the next snippet, where a property named `code` wouldn't have been as indicative of its contents as `colorCode` could be:
464
463
465
464
[source,javascript]
466
465
----
467
-
var { metadata: { gender: characterGender } } = character
466
+
var { color: { code: colorCode } } = palette
468
467
----
469
468
470
469
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.
@@ -473,26 +472,26 @@ Whenever you access a nonexistent property in ES5 notation, you get a value of `
473
472
474
473
[source,javascript]
475
474
----
476
-
console.log(character.boots)
475
+
console.log(palette.luminosity)
477
476
// <- undefined
478
-
console.log(character['boots'])
477
+
console.log(palette['luminosity'])
479
478
// <- undefined
480
479
----
481
480
482
481
With destructuring, the same behavior prevails. When declaring a destructured variable for a property that's missing, you'll get back `undefined` as well.
483
482
484
483
[source,javascript]
485
484
----
486
-
var { boots } = character
487
-
console.log(boots)
485
+
var { luminosity } = palette
486
+
console.log(luminosity)
488
487
// <- undefined
489
488
----
490
489
491
490
A destructured declaration accessing a nested property of a parent object that's `null` or `undefined` will throw an `Exception`, just like regular attempts to access properties of `null` or `undefined` would, in other cases.
492
491
493
492
[source,javascript]
494
493
----
495
-
var { boots: { size } } = character
494
+
var { stroke: { width } } = palette
496
495
// <- Exception
497
496
var { missing } = null
498
497
// <- Exception
@@ -511,37 +510,37 @@ As part of destructuring, you can provide default values for those cases where t
511
510
512
511
[source,javascript]
513
512
----
514
-
var { boots = { size: 10 } } = character
515
-
console.log(boots)
516
-
// <- { size: 10 }
513
+
var { description = 'This is a color palette' } = palette
514
+
console.log(description)
515
+
// <- 'This is a color palette'
517
516
----
518
517
519
518
Default values can also be provided in nested property destructuring.
520
519
521
520
[source,javascript]
522
521
----
523
-
var { metadata: { enemy = 'Satan' } } = character
524
-
console.log(enemy)
525
-
// <- 'Satan'
522
+
var { color: { density = 320 } } = palette
523
+
console.log(density)
524
+
// <- 320
526
525
----
527
526
528
527
For use in combination with aliases, you should place the alias first, and then the default value, as shown next.
529
528
530
529
[source,javascript]
531
530
----
532
-
var { boots: footwear = { size: 10 } } = character
It's possible to use the ((("computed property names")))computed property names syntax in destructuring patterns. In this case, however, you're required to provide an alias to be used as the variable name. That's because computed property names allow arbitrary expressions and thus the compiler wouldn't be able to infer a variable name. In the following example we use the `value` alias, and a computed property name to extract the `boots` property from the `character` object.
534
+
It's possible to use the ((("computed property names")))computed property names syntax in destructuring patterns. In this case, however, you're required to provide an alias to be used as the variable name. That's because computed property names allow arbitrary expressions and thus the compiler wouldn't be able to infer a variable name. In the following example we use the `paletteProfile` alias, and a computed property name to extract the `profile` property from the `palette` object.
536
535
537
536
[source,javascript]
538
537
----
539
-
var { ['boo' + 'ts']: characterBoots } = character
540
-
console.log(characterBoots)
538
+
var { ['pro' + 'file']: paletteProfile } = palette
539
+
console.log(paletteProfile)
541
540
// <- true
542
541
----
543
542
544
-
This flavor of destructuring is probably the least useful, as `characterBoots = character[type]` is usually simpler than `{ [type]: characterBoots } = character`, as it's a more sequential statement. That being said, the feature is useful when you have properties you want to declare in the object literal, as opposed to using subsequent assignment statements.
543
+
This flavor of destructuring is probably the least useful, as `sourceProperty = source[property]` is usually simpler than `{ [property]: sourceProperty } = source`, as it's a more sequential statement. That being said, the feature is useful when you have properties you want to declare in the object literal, as opposed to using subsequent assignment statements.
545
544
546
545
That's it, as far as objects go, in terms of destructuring. ((("assignment destructuring", "objects", startref="ad2o")))((("destructuring", "objects", startref="d2o")))((("objects", "destructuring", startref="o2d")))((("aliases", startref="al2")))What about arrays?
0 commit comments