本文是基于Windows 10系统环境,进行ES6新特性的学习和使用
let arr = [1, 2, 3]
const [a, b, c] = arr
console.log(a) // a= 1
console.log(b) // b= 2
console.log(c) // c= 3
const person = {
name:'xu',
age:20,
}
const { name:newName, age:newAge } = person
console.log(newName) // newName= xu
console.log(newAge ) // newAge = 20
function add(a, b=1){
return a+b;
}
function show(a){
console.log(a);
}
==>
const show2 = a => console.log(a);
function add(a,b){
console.log(a+b);
}
==>
const add2 = (a,b) => console.log(a+b);
function add(a,b){
let c = a + b
return c;
}
==>
const add2 = (a,b) =>{
let c = a+b;
return c;
}
const person = {
name:'xu',
age:20,
}
const hello2 = ( {name} ) => console.log(name);
let arr = [1, 2, 3]
let newArr = arr.map(item => return item+1);
// 返回 [2, 3, 4]
let arr = [1, 2, 3]
let newArr = arr.reduce((a,b)=>return a+b);
// 返回6
console.log(...[1, 2, 3]) // 1,2,3
//数组合并
let arr = [...[1, 2, 3],...[4,5,6]]
console.log(arr) // 1,2,3,4,5,6
//与解构表达式结合
const [first, ...rest] = [1,2,3]
console.log(rest) // 2,3
const p = new Promise((resolve, reject)=>{
setTimeout(()=>{
let num = Math.random();
if(num<0.5){
resolve('success' + num);
} else{
reject('error' + num);
}
});
});
p.then(value=>{
console.log(value);
}).catch(error=>{
console.log(error);
})
const s = new Set([1,2,3,4,4,2]);
console.log(s); // Set(4) {1, 2, 3, 4}
const items = new Set([1,9,4,6]);
const array = Array.from(items);
console.log(array); // [1,9,4,6]
const s = new Set([1,2,3,4,4,2]);
console.log(s.size); // 4
const s = new Set();
s.add(6).add(8).add(8);
console.log(s.size); // 2
console.log(s.has(6)); // true
console.log(s.has(7)); // false
console.log(s.has(8)); // true
const s = new Set();
s.add(6).add(8).add(8);
console.log(s); // Set(2) {6, 8}
s.delete(6);
console.log(s); // Set(2) {8}
const set = new Set([1, 2, 3, 4, 4]);
const result = [...set][1];
console.log(result);
const result= new Set();
result.add(1)
console.log(result);
const result= new Set([2,1,5]);
result.clear()
console.log(result);
function dedupe(array) {
return Array.from(new Set(array));
}
dedupe([1,1,2,3]); // [1,2,3]
// Set中的键=值
let set = new Set(['aaa', 'bbb', 'ccc']);
// 遍历Set中所有的键
for(let item of set.keys()) {
console.log(item); // aaa bbb ccc
}
// 遍历Set中所有的值
for(let item of set.values()) {
console.log(item); // aaa bbb ccc
}
// 遍历Set中所有的键值对
for(let item of set.entries()){
console.log(item); // ["aaa", "aaa"],["bbb", "bbb"],["ccc", "ccc"]
}
// 使用 for of 遍历Set中所有的值
for (let item of set) {
console.log(item); // aaa bbb ccc
}
// 调用forEach()方法
set.forEach((value, key) => {
console.log(key + ' : ' + value)
});
// aaa:aaa bbb:bbb ccc:ccc
let s = new Set([6,7,8]);
// map 将原数组映射成新数组
s = new Set([...s].map(x => x * 2));
console.log(s); // [12,14,16]
// filter返回过滤后的新数组
s = new Set([...s].filter(x => (x % 3) ==0));
console.log(s); //[6]
// 实现并集、交集、差集
let s1 = new Set([6,7,8]);
let s2 = new Set([4,7,6]);
let union = new Set([...s1, ...s2]);
console.log(union);
let intersect = new Set([...s1].filter(x => s2.has(x)));
console.log(intersect);
let difference = new Set([...s1].filter(x => !s2.has(x)));
console.log(difference);
// 利用原Set结构映射出一个新的结构,然后赋值给原来的Set结构
let s1 = new Set([1,2,3]);
s1 = new Set([...s1].map(val => val *2));
console.log(s1);
// 利用Array.from
let s2 = new Set([1,2,3]);
s2 = new Set(Array.from(s2, val => val * 2));
console.log(s2);