擴充運算子用三個點號表示,功能是把數組或類別數組物件展開成一系列用逗號隔開的值,擴充運算子有幾點作用,接下來為大家一一解開! ! !
一,展開數組
//展開數組let a = [1,2,3,4,5],b = [...a,6,7];console. log(b);//印出來的值[1, 2, 3, 4, 5, 6, 7]
二,數組的拷貝
//數組的拷貝var c = [1, 2, 3];var d = [...c]; d.push(4); console.log(d);//印出來的值[1, 2, 3, 4 ]
三,陣列的合併
//陣列的合併var j = [7, 1, 2];var k = [5, 0, 8];j = [...k, ...j];console.log(j)//印出來的值[5, 0, 8, 7, 1, 2]
#四,展開函數的呼叫
//展開函數呼叫function fn(a,b,c,d){
console.log(a+b+c+d);}var p=[1,9, 3,,6];let result=fn(5,...p);開函數的呼叫//印出來的值18
擴充運算子(spread)是三個點( ……),將一個陣列||類數組||字串轉為用逗號分隔的序列。這傢伙是用來對陣列進行操作的,把陣列裡面的東西統統拿出來
let zzz=[2,4,6]; console.log(zzz);//[2, 4, 6] console.log(...zzz);//2 4 6 let a=[1,2,3]; let b=[...a,4,5,6]; console.log(b);//1,2,3,4,5,6 let [a,b,...c]=[1,2,3,4,5]; console.log(a,b);//1 2 console.log(c);//[3, 4, 5]
let say333=()=>{ console.log("333");//333 } say333(); (name)=>{ console.log(name); } (a,b)=>{ return a+b; } (a,b)=> a+b;
let aa=(name='wwrs')=>{ console.log(`Hello ${name}`); } aa();//Hello wwrs aa('sss');//Hello sss let bb=(a,b,c)=>{ console.log(a+b+c);//9 } let dd=[2,3,4]; bb(...dd); let he=(a,b,c,d)=>{ console.log(a+b+c+d);//10 } he(1,2,3,4) let he1=(s,j,...shi)=>{ console.log(shi);//[3, 4] } he1(1,2,3,4)
以上是JavaScript幾點基礎講解的詳細內容。更多資訊請關注PHP中文網其他相關文章!