1. 擴展運算符: 2. 箭頭函數: 3. 擴展參數(預設參數,擴展參數,剩餘函數): //預設參數 let sayHellow = (name,age=30)= { let str = console.log(str); } sayHellow("songze",25);//控制台列印songze ...
- 擴展運算符:
擴展運算符有以下四種:
//展開數組
let arr = [1,2,3,4];
let a = [...arr];
console.log(a);
let b = [7,8,9,10];
let g = [1,2,...b,4,5];
console.log(g);
//數組的拷貝
let a = [1,2,3];
let a1 = [...a];
a1.push(4);
console.log(a);
console.log(a1);
//數組的合併
let a = [1,2,3];
let a1 = [4,5,6];
a = [...a,...a1];
console.d(a);
//數組的調用
function fn(a,b,c,d){
console.log(a+b+c+d);
}
var p=[1,2,3,4,5,6];
let result=fn(10,...p);
- 箭頭函數:
//箭頭函數
let a = () => {
console.log("Hello word")
}
a();
//有參數
(name)=>{
console.log(name);
};
//沒有參數
()=>{
console.log("hi word")
};
d('jiang');
//兩個參數
(name,age)=>{
console.log(name,age);
};
//省略後的
let c (a,b)=>a+b;
(a,b)=>{console.log(a+b);
console.log(c);
//可以省略()
let d = name=>{
console.log(name);
}
註釋:箭頭函數解決了定時器里的this指針問題。
- 擴展參數(預設參數,擴展參數,剩餘函數):
//預設參數
let sayHellow = (name,age=30)=>{
let str = `${name} is ${age} years old`
console.log(str);
}
sayHellow("songze",25);//控制台列印songze is 25 years old。
//擴展參數
let sum = (a,b,c) => {
console.log(a+b+c);
};
let arr = [1,2,3];
sum(...arr);//控制台列印6。
//剩餘函數
let he = (a,b,...rest)=>{
console.log(rest);//控制台列印[3, 4, 5, 6, 7, 8, 9]。
console.log(a+b);//控制台列印3.
}
he(1,2,3,4,5,6,7,8,9);