Skip to content

Commit 6eb381c

Browse files
authored
fixes #54 (#55)
1 parent a78153a commit 6eb381c

File tree

1 file changed

+36
-37
lines changed

1 file changed

+36
-37
lines changed

ch02.asciidoc

+36-37
Original file line numberDiff line numberDiff line change
@@ -401,70 +401,69 @@ This ((("arrow functions", startref="af2")))((("arrow functions", "merits and us
401401

402402
==== Destructuring Objects
403403

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:
405405

406406
[source,javascript]
407407
----
408-
var character = {
409-
name: 'Bruce',
410-
pseudonym: 'Batman',
411-
metadata: {
412-
age: 34,
413-
gender: 'male'
408+
var palette = {
409+
profile: 'intense-red',
410+
name: 'Red',
411+
color: {
412+
code: `#f00`
414413
},
415-
batarang: ['gas pellet', 'bat-mobile control', 'bat-cuffs']
414+
luminosity: 0.8
416415
}
417416
----
418417

419-
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:
420419

421420
[source,javascript]
422421
----
423-
var pseudonym = character.pseudonym
422+
var profile = palette.profile
424423
----
425424

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:
427426

428427
[source,javascript]
429428
----
430-
var { pseudonym } = character
429+
var { profile } = palette
431430
----
432431

433432
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:
434433

435434
[source,javascript]
436435
----
437-
var { pseudonym, name } = character
436+
var { profile, name } = palette
438437
----
439438

440439
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:
441440

442441
[source,javascript]
443442
----
444-
var { pseudonym } = character, two = 2
443+
var { profile } = palette, two = 2
445444
----
446445

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:
448447

449448
[source,javascript]
450449
----
451-
var { pseudonym: alias } = character
452-
console.log(alias)
453-
// <- 'Batman'
450+
var { profile: id } = palette
451+
console.log(id)
452+
// <- 'intense-red'
454453
----
455454

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:
457456

458457
[source,javascript]
459458
----
460-
var { metadata: { gender } } = character
459+
var { color: { code } } = palette
461460
----
462461

463462
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:
464463

465464
[source,javascript]
466465
----
467-
var { metadata: { gender: characterGender } } = character
466+
var { color: { code: colorCode } } = palette
468467
----
469468

470469
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 `
473472

474473
[source,javascript]
475474
----
476-
console.log(character.boots)
475+
console.log(palette.luminosity)
477476
// <- undefined
478-
console.log(character['boots'])
477+
console.log(palette['luminosity'])
479478
// <- undefined
480479
----
481480

482481
With destructuring, the same behavior prevails. When declaring a destructured variable for a property that's missing, you'll get back `undefined` as well.
483482

484483
[source,javascript]
485484
----
486-
var { boots } = character
487-
console.log(boots)
485+
var { luminosity } = palette
486+
console.log(luminosity)
488487
// <- undefined
489488
----
490489

491490
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.
492491

493492
[source,javascript]
494493
----
495-
var { boots: { size } } = character
494+
var { stroke: { width } } = palette
496495
// <- Exception
497496
var { missing } = null
498497
// <- Exception
@@ -511,37 +510,37 @@ As part of destructuring, you can provide default values for those cases where t
511510

512511
[source,javascript]
513512
----
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'
517516
----
518517

519518
Default values can also be provided in nested property destructuring.
520519

521520
[source,javascript]
522521
----
523-
var { metadata: { enemy = 'Satan' } } = character
524-
console.log(enemy)
525-
// <- 'Satan'
522+
var { color: { density = 320 } } = palette
523+
console.log(density)
524+
// <- 320
526525
----
527526

528527
For use in combination with aliases, you should place the alias first, and then the default value, as shown next.
529528

530529
[source,javascript]
531530
----
532-
var { boots: footwear = { size: 10 } } = character
531+
var { color: paletteColor = { density: 320 } } = palette
533532
----
534533

535-
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.
536535

537536
[source,javascript]
538537
----
539-
var { ['boo' + 'ts']: characterBoots } = character
540-
console.log(characterBoots)
538+
var { ['pro' + 'file']: paletteProfile } = palette
539+
console.log(paletteProfile)
541540
// <- true
542541
----
543542

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.
545544

546545
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?
547546

0 commit comments

Comments
 (0)