ES6 基础
This commit is contained in:
46
code/ES6/src/05_destructuring/01_object_destructuring.js
Normal file
46
code/ES6/src/05_destructuring/01_object_destructuring.js
Normal file
@ -0,0 +1,46 @@
|
||||
// 1.对象结构
|
||||
(() => {
|
||||
let person = {
|
||||
name: "heibaiying",
|
||||
age: 18
|
||||
};
|
||||
let {name, age} = person;
|
||||
console.log(name); // "heibaiying"
|
||||
console.log(age); // "18"
|
||||
})();
|
||||
|
||||
//2.传递默认值
|
||||
(() => {
|
||||
let person = {
|
||||
name: "heibaiying",
|
||||
age: 18
|
||||
};
|
||||
let {name, age, occupation = "programmer"} = person;
|
||||
console.log(occupation); // programmer
|
||||
})();
|
||||
|
||||
|
||||
//3.改变变量名
|
||||
(() => {
|
||||
let person = {
|
||||
name: "heibaiying",
|
||||
age: 18
|
||||
};
|
||||
let {name: myName, age: myAge, occ: occupation = "programmer"} = person;
|
||||
console.log(myName);
|
||||
console.log(myAge);
|
||||
console.log(occupation);
|
||||
})();
|
||||
|
||||
//4.支持嵌套
|
||||
(() => {
|
||||
let person = {
|
||||
name: "heibaiying",
|
||||
age: 18,
|
||||
teacher: {
|
||||
name: "heibai"
|
||||
}
|
||||
};
|
||||
let {teacher: {name: teacherName}} = person;
|
||||
console.log(teacherName);
|
||||
})();
|
56
code/ES6/src/05_destructuring/02_array_destructuring.js
Normal file
56
code/ES6/src/05_destructuring/02_array_destructuring.js
Normal 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]"
|
||||
})();
|
||||
|
22
code/ES6/src/05_destructuring/03_mixed_destructuring.js
Normal file
22
code/ES6/src/05_destructuring/03_mixed_destructuring.js
Normal file
@ -0,0 +1,22 @@
|
||||
let node = {
|
||||
type: "Identifier",
|
||||
name: "foo",
|
||||
loc: {
|
||||
start: {
|
||||
line: 1,
|
||||
column: 1
|
||||
},
|
||||
end: {
|
||||
line: 1,
|
||||
column: 4
|
||||
}
|
||||
},
|
||||
range: [0, 3]
|
||||
};
|
||||
let {
|
||||
loc: {start},
|
||||
range: [startIndex]
|
||||
} = node;
|
||||
console.log(start.line); // 1
|
||||
console.log(start.column); // 1
|
||||
console.log(startIndex); // 0
|
20
code/ES6/src/05_destructuring/04_parameters_destructuring.js
Normal file
20
code/ES6/src/05_destructuring/04_parameters_destructuring.js
Normal file
@ -0,0 +1,20 @@
|
||||
// 1.参数解构
|
||||
function setCookie01(name, value, {secure, path, domain, expires} = {}) {
|
||||
console.log(`secure: ${secure}, path: ${path}, domain:${domain}, expires:${expires}`)
|
||||
}
|
||||
|
||||
setCookie01("type", "js", {
|
||||
secure: true,
|
||||
expires: 60000
|
||||
});
|
||||
|
||||
// 2.参数解构默认值
|
||||
function setCookie02(name, value,
|
||||
{
|
||||
secure = false,
|
||||
path = "/",
|
||||
domain = "example.com",
|
||||
expires = new Date(Date.now() + 360000000)
|
||||
} = {}
|
||||
) {
|
||||
}
|
Reference in New Issue
Block a user