首页 > 其他 > 详细

es6-扩展运算符和rest运算符

时间:2019-06-22 10:39:06      阅读:106      评论:0      收藏:0      [点我收藏+]

es6-扩展运算符和rest运算符

扩展运算符:不确定他的参数个数时使用运算扩展符

// 声明一个方法 但不确定他的参数个数时使用对象运算扩展符
function ananiha(...arg){
        console.log(arg[0]); //1
        console.log(arg[1]); //2
        console.log(arg[2]); //3
        console.log(arg[3]); //undefined
}
ananiha(1,2,3);

// 例子
let arr1 = [‘www‘,‘anan‘,‘com‘];
let arr2 = arr1;    //不开辟新的内存空间 把arr2的内存空间映射到了arr1
console.log(arr2);

arr2.push(‘roger‘);
console.log(arr1);

// 用扩展运算符去解决
let arr1 = [‘www‘,‘anan‘,‘com‘];
let arr2 = [...arr1]; //赋值arr1中的每一个值 
console.log(arr2); //没有改变

arr2.push(‘roger‘);
console.log(arr2); //改变了
console.log(arr1);  //没有改变
rest运算符:
// rest运算符
// rest...
//rest 英文翻译:剩余

function anan(first,...arr){
    // console.log(arr.length);
    // for of 循环提高效率
    for(let val of arr){
        console.log(val);
    }
}
anan(1,3,4,5,6,8); // 5  打印出来我们不确定的参数的length

 

 

es6-扩展运算符和rest运算符

原文:https://www.cnblogs.com/Ananiah/p/11067601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!