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: manuscript/01-The-Basics.md
+172Lines changed: 172 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -628,6 +628,178 @@ const age = 30;
628
628
629
629
W> Several browsers implement pre-ECMAScript 6 versions of `const`. Implementations range from being simply a synonym for `var` (allowing the value to be overwritten) to actually defining constants but only in the global or function scope. For this reason, be especially careful with using `const` in a production system. It may not be providing you with the functionality you expect.
630
630
631
+
## Destructuring Assignment
632
+
633
+
JavaScript developers spend a lot of time pulling data out of objects and arrays. It's not uncommon to see code such as this:
634
+
635
+
```js
636
+
var options = {
637
+
repeat:true,
638
+
save:false
639
+
};
640
+
641
+
// later
642
+
643
+
var localRepeat =options.repeat,
644
+
localSave =options.save;
645
+
```
646
+
647
+
Frequently, object properties are stored into local variables for more succinct code and easier access. ECMAScript 6 makes this easy by introducing *destructuring assignment*, which systematically goes through an object or array and stores specified pieces of data into local variables.
648
+
649
+
### Object Destructuring
650
+
651
+
Object destructuring assignment syntax uses an object literal on the left side of an assignment operation. For example:
652
+
653
+
```js
654
+
var options = {
655
+
repeat:true,
656
+
save:false
657
+
};
658
+
659
+
// later
660
+
661
+
var { repeat: localRepeat, save: localSave } = options;
662
+
663
+
console.log(localRepeat); // true
664
+
console.log(localSave); // false
665
+
```
666
+
667
+
In this code, the value of `options.repeat` is stored in a variable called `localRepeat` and the value of `options.save` is stoed in a variable called `localSave`. These are both specified using the object literal syntax where the key is the property to find on `options` and the value is the variable in which to store the property value.
668
+
669
+
I> If the property with the given name doesn't exist on the object, then the local variable gets a value of `undefined`.
670
+
671
+
If you want to use the property name as the local variable name, you can omit the colon and the identifier, such as:
672
+
673
+
```js
674
+
var options = {
675
+
repeat:true,
676
+
save:false
677
+
};
678
+
679
+
// later
680
+
681
+
var { repeat, save } = options;
682
+
683
+
console.log(repeat); // true
684
+
console.log(save); // false
685
+
```
686
+
687
+
Here, two local variables called `repeat` and `save` are created. They are initialized with the value of `options.repeat` and `options.save`, respectively. This shorthand is helpful when there's no need to have different variable names.
688
+
689
+
Destructuring can also handled nested objects, such as the following:
690
+
691
+
```js
692
+
var options = {
693
+
repeat:true,
694
+
save:false,
695
+
rules: {
696
+
custom:10,
697
+
}
698
+
};
699
+
700
+
// later
701
+
702
+
var { repeat, save, rules: { custom }} = options;
703
+
704
+
console.log(repeat); // true
705
+
console.log(save); // false
706
+
console.log(custom); // 10
707
+
```
708
+
709
+
In this example, the `custom` property is embedded in another object. The extra set of curly braces allows you to descend into a nested an object and pull out its properties.
710
+
711
+
A> #### Syntax Gotcha
712
+
A>
713
+
A> If you try use destructuring assignment without a `var`, `let`, or `const`, you may be surprised by the result:
714
+
A>
715
+
A> ```js
716
+
A> // syntax error
717
+
A> { repeat, save, rules: { custom }} = options;
718
+
A> ```
719
+
A>
720
+
A> This causes a syntax error because the opening curly brace is normally the beginning of a block and blocks can't be part of assignment expressions.
721
+
A>
722
+
A> The solution is to wrap the left side literal in parentheses:
723
+
A>
724
+
A> ```js
725
+
A> // no syntax error
726
+
A> ({ repeat, save, rules: { custom }}) = options;
727
+
A> ```
728
+
A>
729
+
A> This now works without any problems.
730
+
731
+
### Array Destructuring
732
+
733
+
Similarly, you can destructure arrays using array literal syntax on the left side of an assignment operation. For example:
734
+
735
+
```js
736
+
var colors = [ "red", "green", "blue" ];
737
+
738
+
// later
739
+
740
+
var [ firstColor, secondColor ] = colors;
741
+
742
+
console.log(firstColor); // "red"
743
+
console.log(secondColor); // "green"
744
+
```
745
+
746
+
In this example, array destructuring pulls out the first and second values in the `colors` array. Keep in mind that the array itself isn't change in any way.
747
+
748
+
Similar to object destructuring, you can also nest array destructuring. Just use another set of square brackets to descend into a subarray:
Here, the `secondColor` variable refers to the `"green"` value inside of the `colors` array. That item is contained within a second array, so the extra square brackets around `secondColor` in the destructuring assignment is necessary.
762
+
763
+
### Mixed Destructuring
764
+
765
+
It's possible to mix objects and arrays together in a destructuring assignment expression using a mix of object and array literals. For example:
This example extracts two property values, `repeat` and `save`, and then two items from the `colors` array, `firstColor` and `secondColor`. Of course, you could also choose to retrieve the entire array:
783
+
784
+
```js
785
+
var options = {
786
+
repeat:true,
787
+
save:false,
788
+
colors: [ "red", "green", "blue" ]
789
+
};
790
+
791
+
var { repeat, save, colors } = options;
792
+
793
+
console.log(repeat); // true
794
+
console.log(save); // false
795
+
console.log(colors); // "red,green,blue"
796
+
console.log(colors ===options.colors); // true
797
+
```
798
+
799
+
This modified example retrieves `options.colors` and stores it in the `colors` variable. Notice that `colors` is a direct reference to `options.colors` and not a copy.
800
+
801
+
Mixed destructuring is very useful for pulling values out of JSON configuration structures without navigating the entire structure.
802
+
631
803
## Numbers
632
804
633
805
JavaScript numbers can be particularly complex due to the dual usage of a single type for both integers and floats. Numbers are stored in the IEEE 754 double precision floating point format, and that same format is used to represent both types of numbers. As one of the foundational data types of JavaScript (along with strings and booleans), numbers are quite important to JavaScript developers. Given the new emphasis on gaming and graphics in JavaScript, ECMAScript 6 sought to make working with numbers easier and more powerful.
0 commit comments