<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>对应位置</title> <script src="../../../vendor/traceur.js"></script> <script src="../../../vendor/bootstrap.js"></script> <script type="text/traceur"> // ES6允许按照一定模式,从数组和对象中提取值,对变量进行赋值,这被称为解构(Destructuring) 。 // ES5 var a = 1; var b = 2; var c = 3; // ES6 var [a, b, c] = [1, 2, 3]; console.log(a); //1 console.log(b); //2 console.log(c); //3 -------------------------------------------------------- let [foo, [[bar], base]] = [1, [[2], 3]]; console.log(foo); //1 console.log(bar); //2 console.log(base); //3 let [, , third] = ["first", "second", "third"]; console.log(third); //third let [one, , three] = ["One", "Two", "Three"]; console.log(one); //One console.log(three); //Three let [head, ...tail] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(head); //0 console.log(tail); //[1, 2, 3, 4, 5, 6, 7, 8, 9] let [head, tail] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; console.log(head); //0 console.log(tail); //1 ---------------------------------------------------- var [temp] = []; console.log(temp); //undefined var [first, second] = [100]; console.log(first); //100 console.log(second);//undefined ---------------------------------------------------- let [x, y] = [1, 2, 3]; console.log(x); //1 console.log(y); //2 let [a, [b], c] = [1, [2, 3], 4]; console.log(a); //1 console.log(b); //2 console.log(c); //4 ----------------------------------------------- var [temp = "string"] = []; console.log(temp); //string,没有值走默认 var [temp = "string"] = ["tempString"]; console.log(temp); //tempString,默认值和指定值冲突(指定值不为未定义)用指定值、指定值为未定义用默认值, var [x = "aaa", y] = ["bbb"]; console.log(x); //bbb console.log(y); //undefined var [m, n = "aaa"] = ["bbb"]; console.log(m); //bbb console.log(n); //aaa var [p, q = "aaa"] = ["bbb", undefined]; console.log(p); //bbb console.log(q); //aaa ------------------------------------------------------- var [temp] = 1; //正确写法:var [temp] = [1]; 报错1[Symbol.iterator] is not a function at eval var [temp] = false; //报错:false[Symbol.iterator] is not a function at eval var [temp] = NaN; //报错:NaN[Symbol.iterator] is not a function at eval var [temp] = undefined; //报错:Cannot read property 'Symbol(Symbol.iterator)' of undefined at eval var [temp] = null; //报错:Cannot read property 'Symbol(Symbol.iterator)' of null at eval ------------------------------------------------------- </script> </head> <body> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Iterator接口</title> <script src="../../../vendor/traceur.js"></script> <script src="../../../vendor/bootstrap.js"></script> <script type="text/traceur"> //只要某种数据结构具有Iterator接口,都可以采用数组形式的解构赋值。 let [a, b, c] = new Set(["a", "b", "c"]); console.log(a); //a console.log(b); //b console.log(c); //c function* fibs() { let a = 0; let b = 1; while (true) { yield a; [a, b] = [b, a + b]; } }; var [first, second, third, fourth, fifth, sixth] = fibs(); console.log(sixth); //5 </script> </head> <body> </body> </html>