赞
踩
三个点(...)真名叫扩展运算符,是在ES6中新增加的内容,它可以在函数调用/数组构造时,将数组表达式或者string在语法层面展开;还可以在构造字面量对象时将对象表达式按照key-value的方式展开
字面量一般指[1,2,3]或者{name:'张三'}这种简洁的构造方式,多层嵌套的数组和对象三个点就无能为力了
运用场景
数组 || 对象的复制
- //数组的复制
- var arr1 = ['hello']
- var arr2 =[...arr1]
- arr2 // ['hello']
- //对象的复制
- var obj1 = {name:'zhangsan'}
- var obj2 ={...obj1}
- ob12 // {name:'zhangsan'}
数组 || 对象的合并
- //数组的合并
- var arr1 = ['hello']
- var arr2 =['zhangsan']
- var mergeArr = [...arr1,...arr2]
- mergeArr // ['hello','zhangsan']
- // 对象分合并
- var obj1 = {name:'zhangsan'}
- var obj2 = {height:176}
- var mergeObj = {...obj1,...obj2}
- mergeObj // {name: "zhangsan", height: 176}
- function f(v,w,x,y,z){ }
- var args = [2,3]
- f(1,...args,4,...[5])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。