ES6 基础

This commit is contained in:
罗祥
2019-12-15 19:03:36 +08:00
parent f0f3336641
commit 0b506230fe
24 changed files with 1858 additions and 11 deletions

View File

@ -0,0 +1,56 @@
// 1.数组解构
(() => {
let colors = ["red", "green", "blue"];
let [firstColor, secondColor] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"
})();
(() => {
let colors = ["red", "green", "blue"];
let [, , thirdColor] = colors;
console.log(thirdColor); // "blue
})();
//3.互换值
let a = 1,
b = 2;
[a, b] = [b, a];
console.log(a); // 2
console.log(b); // 1
// 4.默认值
(() => {
let [firstColor, secondColor = "green"] = ["red"];
console.log(firstColor); // "red"
console.log(secondColor); // "green"
})();
// 5.嵌套解构
(() => {
let colors = ["red", ["green", "lightgreen"], "blue"];
let [firstColor, [secondColor]] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"
})();
// 6.剩余项解构
(() => {
let colors = ["red", "green", "blue"];
let [firstColor, ...restColors] = colors;
console.log(firstColor); // "red"
console.log(restColors.length); // 2
console.log(restColors[0]); // "green"
console.log(restColors[1]); // "blue"
})();
// 7.在 ES6 中克隆数组
(() => {
let colors = ["red", "green", "blue"];
let [...clonedColors] = colors;
console.log(clonedColors); //"[red,green,blue]"
})();