扩展运算符用三个点号表示,功能是把数组或类数组对象展开成一系列用逗号隔开的值,扩展运算符有几点作用,接下来为大家一一解开!!!
一,展开数组
//展开数组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中文网其他相关文章!