Deconstruction assignment of ES6 learning variables

Deconstruction assignment of the variable

ES6 is allowed to be extracted from the array and objects in accordance with a certain mode, and the variable is assigned.

Deconstructed value of the array

VAR [A, B, C] = [1, 2, 3]; A // 1; B // 2; C // 3;
 The above code indicates that the value can be extracted from the array, and the variable is assigned according to the corresponding relationship of the position.   
In essence, this way is “mode match”, as long as the mode is the same as the two sides of the equal number, the corresponding value is given the corresponding value.

Let [foo,[[[bar], baz] = [1,[[2], 3]; foo // 1; bar // 2; Baz // 3; let [,, third] = [‘foo’, ‘bar’, ‘baz’]; third // ‘Baz’let [head, … tail] = [1, 2, 3, 4 HEAD / / 1; TAIL / / [2, 3, 4]

If the deconstruction is unsuccessful, the value of the variable is equal to undefined.

Let [x, y, z] = [‘a’] x // ‘a’; y // undefinedZ // []
   
Incomplete Deconstruction

If the pattern left on the left side only matches a part of the number of equal numbers, the deconstruction is still successful, this is not complete Deconstruction.
  Let [x, y] = [1, 2, 3] x // 1Y // 2  

If the right side is not an array, it will be an error. (There is no structure that can be traverse). As long as some data structure has an Iterator interface, the deconstruction assignment of the array can be used

function * fibs () {var A = 0; var B = 1; while (true) {yield a; [a, b] = [b, a + b];}} var [first, second, third, fous, fiffh, sixth] = fibs (); sixth /// 5
   
FIBS is a Generator function, native has a Iterator interface, and the deconsive assignment will be obtained from this interface.
Default

Deconstruction assignment allows the default value to be specified.

VAR [foo = true] = []; foo // true
   ES6 internal use strict use is equal to The operator (===) determines if a location has a value. So, if an array member is not strictly equal to undefined, the default value will not take effect. 
VAR [x = 1] = [undefined] x // 1VAR [x = 1] = [null] x // null

The expression can also be used as a default. If the expression is used as the default value, the expression is inert evaluation, and only the value is obtained when it is used.

Function f () {Console.log (‘aaa’)} let [x = f ()] = [1]
  In the above code, the function f does not execute, because X can be taken to the value.  
The default value can reference other variables of the deconstructed assignment, but the variable must have declared. The deconstruction assignment of the object

deconstruction assignment can be applied to an array, and it can also be used for objects.

Var {foo, bar} = {foo: ‘aaa’, bar: ‘bbb’} foo // ‘aaa’ bar // ‘bbb’
   
The deconstruction assignment of the object has a difference between the array, that is, the elements of the array are sorted in order, the value of the variable is determined by its position, and the attribute of the object is not in the order, The variable must be the same as the attribute to get the correct value.

Var {bar, foo} = {foo: ‘aaa’, bar: ‘bbb’} foo // ‘aaa’bar //’ bb’var {baz } = {foo: ‘AAA’, BAR: ‘BBB’} Baz // Undefined

If the variable name is inconsistent with the attribute name, it must be written as this:
  VAR {foo: baz} = {foo: 'aaa'} baz // 'aaa'  

In actually object The deconstruction assignment is the following form:

VAR {foo: foo, bar: bar} = {foo: ‘aaa’, bar: ‘bbb’}

The internal mechanism of the deconstruction assignment of the object is to find the same name attribute, and then assign the corresponding variable, which is really assigned, not the former.
 However, when taking the above writing, the declaration and assignment of the variable is together. For Let and Const, the variable cannot be re-declared, so once the value is not previously declared, it will report an error.  The deconstruction of the object can also be used for the nested structure.  
VAR OBJ = {P: [‘Hello’, {Y: ‘World’}]} var {p: [x, {y}]} = Objx / / ‘Hello’Y //’ WORLD ‘

This time P is mode, not a variable, so it is not assigned.

The deconstruction assignment of the object can also specify the default value. The effective condition of the default value is that the target value is strictly equal to undefined.
  VAR {x = 3} = {x: undefined} x // 3Var {x = 3} = {x: null} x // null 
If the deconstruction mode is a nested object, it will be an error thereof if the parent attribute where the child object is located.

VAR {foo: {bar}} = {baz: ‘baz’}
   Summary 
The above is the full content of this article, I hope this paper will bring some help to everyone or use ES6. If you have any questions, you can leave a message to communicate.

© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment