The previous article on the deconstruction of object, this talk about array deconstruction, the concept of the same, but the writing will be somewhat different, first look at a simple example:
let [x, y] = [' A ', ' B ', ' C ']; x = ' a '; y = ' B '
As with the object deconstruction, we first discuss what conditions the assignment expression should have on the right side to be able to deconstruct the assignment, and the specification 12.14.5.2 has this description:
arrayassignmentpattern: []
1. Let iterator be getiterator (value).
2. Returnifabrupt (iterator).
3. Return Iteratorclose (iterator, normalcompletion (empty)).
From the above description, the right should be a iterator object, specific what kind of object can be regarded as iterator, the later chapters will be detailed, here is not carefully said, first remember it.
Let X;
[x] = {}; TypeError, empty objects are not iterable
[x] = undefined;//TypeError, not iterable
[x] = null;//TypeError, Not iterable
The above example illustrates the object, Undefined,null is not a iterator object.
Here's a look at some of the characteristics of array deconstruction:
let [x] = []; x = undefined
var o = {},a =[];
[O.a, O.B, o.c,a[0]] = [1,2,3,4];
o:{a:1, B:2, C:3},a:[4] Let
[x,... y] = ' abc ';//x= ' a '; y=[' B ', ' C '],rest operator
var o = [1,2,3],
A, B, C, P;
p = [,, c] = [a,b] = o;//destructors continuous assignment
trace (A + "" + B + "" +c); 1 2 3
trace (p = = O);/true
var a1 = [1, [2, 3, 4,7,8], 5];//nested deconstruction +rest operator
var [A, [B, C,... d], E] = A1;
A=1,b=2,c=3,d=[4,7,8],e=5
Let's look at how to use an array to deconstruct the values of two variables:
var x = ten, y =;
[y, X] = [x, y];//so elegant
//If also with the help of temporary variables, but also can use the XOR operator, of course, not understand
x^=y;
y^=x;
X^=y;
The default values are used when the object is refactored, and the same array deconstruction can have default values:
let [x=3, y] = []; x = 3; y = undefined let
[x=1] = [undefined];//x = 1
function log (x) {trace (x+ "\ n"); return ' YES '} Let
[A=log (' He Llo ')] = [1];//a = 1 Let
[A=log (' hello ')] = [0];//a = ' YES ', note the difference between Kinoma and ES6 norms here, under Firefox43, a=0 let
[x=3, y=x] = [ ]; x=3; y=3 let
[x=3, y=x] = [7];//x=7; y=7
so [x=3, y=x] = [7, 2];//x=7; y=2 let
[X=y, y=3] = [];//Referenceer Ror, left to right evaluate let
[{prop:x} = {}] = [];//x=undefined let
[{prop:x} = {}] = [1];//x=undefined
let [{prop:x} = {prop:123}] = [];//x=123
let [{prop:x} = {prop:123}] = [{prop:1}];//x=1
Having described the above features, let's look at some examples of actual use:
Let [All, year, month, day] =/^ (\d\d\d\d)-(\d\d)-(\d\d) $/.exec (' 2015-12-31 ');
All= "2015-12-31", year= "2015", month= "a", day= "to" let
[,,, Day] =/^ (\d\d\d\d)-(\d\d)-(\d\d) $/.exec ("Test") | | [];
day=undefined, if not write | | [], an error will be made because EXEC returns null and NULL is not possible to deconstruct
* All of the above code passed the test in Kinoma studio