ES6学习之路3----rest参数与扩展运算符
来源:互联网 发布:云计算产业链市场份额 编辑:程序博客网 时间:2024/05/20 13:15
什么是rest参数(…rest)
ES6引入rest参数(形式为“…变量名”),用于获取函数的多余参数。rest参数之后不能再有其他参数(即只能是最后一个参数)。
函数的rest参数
ES5写法:
function fn(){ for(var i = 0; i<arguments.length ; i++){ console.log(arguments[i]); }}fn(1,2,3);//1,2,3
ES6写法:
function fn(...arg){ arg.forEach(current => console.log(current));}fn(1,2,3);//1,2,3
实例1:
function fn(...rest){console.log(rest);}fn(1);//[1]fn(1,2,3,4);//[1,2,3,4]fn.length;//0
实例2:
function fn(a,...rest){ console.log(a); console.log(rest);}fn(1);//1 []fn(1,2,3,4);//1 [2,3,4]fn.length;//1
实例3:
function fn(a,b,...rest){ console.log(a); console.log(b); console.log(rest);}fn(1);//1 undefined []fn(1,2,3,4);//1 2 [3,4]fn.length;//2
实例4:
function fn(a,...rest,b){ console.log(a); console.log(b); console.log(rest);}fn(1);fn(1,2,3,4);fn.length;//报错:Uncaught SyntaxError: Rest parameter must be last formal parameter
实例5:
function fn(...rest) { console.log(rest); console.log(arguments);}fn();//[]//[callee: (...), Symbol(Symbol.iterator): ƒ]fn(1,2);//[1,2]//[1,2,callee: (...), Symbol(Symbol.iterator): ƒ]
此处是谷歌浏览器测试的结果。ECMAScript6函数剩余参数(Rest Parameters)写到在Firefox浏览器会报错,所以在Firefox,(…rest)和arguments不能同时使用!
数组的rest参数
实例1:
let [a,...rest] = [1,2,3,4];console.log(a);//1console.log(rest);//[2,3,4]
实例2:
let [a,...rest] = [1];console.log(a);//1console.log(rest);//[]
实例3:
let [a,...rest,b] = [1,2,3,4];console.log(a);console.log(rest);console.log(b);//直接报错:Uncaught SyntaxError: Rest element must be last element
通过以上实例我们可以总结出:
- rest参数是一个真正的数组,它可使用Array.prototype上的所有方法;
- 对函数使用length属性时,不包含rest参数;
- rest参数只能作为最后一个参数,在它之后不能再有其他参数,否则会报错。
什么是扩展运算符(…)
扩展运算符可以看做是 rest 参数的逆运算,将一个数组转为参数序列。
实例1:
let arr = [1,2,3,4];console.log(...arr);//1 2 3 4
实例2:
let arr = [1,2,3,4];console.log(1,...arr,2);//1 1 2 3 4 2
实例3:
function add(...rest){ let sum = 0; rest.forEach(current => sum += current); return sum;}add(...[1,2,3,4,5,6]);//21
替代 apply 方法
实例1:
## Math.maxES5:Math.max.apply(null,[1,2,3,5,6,7,4,5]);//7ES6:Math.max(...[1,2,3,5,6,7,4,5]);//7## Array.prototype.pushES5:let arr = [1,2,3],arr0 = [6,7,8];Array.prototype.push.apply(arr,arr0);console.log(arr);//[1, 2, 3, 6, 7, 8]ES6:arr.push(...arr0);console.log(arr);//[1, 2, 3, 6, 7, 8]
字符串转为数组
实例:
let str = 'prototype';ES5:console.log(str.split(''));//["p", "r", "o", "t", "o", "t", "y", "p", "e"]ES6:console.log(...str);//p r o t o t y p econsole.log([...str]);//["p", "r", "o", "t", "o", "t", "y", "p", "e"]
任何 Iterator 接口的对象,都可以用扩展运算符转为真正的数组。
总结:
rest参数
- rest参数是将一个参数序列转化为数组;
- rest参数是一个真正的数组;
- rest参数可使用Array.prototype上的所有方法;
- 对函数使用length属性时,不包含rest参数;
- rest参数只能作为最后一个参数,在它之后不能再有其他参数,否则会报错;
- 在Firefox浏览器,rest参数和arguments不能同时使用,同时使用会报错。
扩展运算符
- 任何 Iterator 接口的对象,都可以用扩展运算符转为真正的数组;
- 扩展运算符将一个数组转为参数序列。
阅读全文
0 0
- ES6学习之路3----rest参数与扩展运算符
- ES6 rest参数和扩展运算符
- ES6-rest参数和扩展运算符
- ES6-rest参数和扩展运算符
- ES6(六: 函数扩展)(默认值,rest参数,扩展运算符)
- 在es6中,对rest参数和扩展运算符的理解(待更新)
- ECMAScript6(ES6)标准之函数扩展特性箭头函数、Rest参数及展开操作符
- ES6-函数的扩展-rest参数
- ES6学习之路(三) 字符串扩展
- es6的扩展运算符
- 10、函数的扩展 为函数参数指定默认值、函数的 rest 参数、箭头函数—ES6学习笔记
- ES6---扩展运算符和rest‘...’(三点运算符),在数组、函数、set/map等中的应用
- ES6 解构运算符 rest运算符,...运算符
- ES6-数组的扩展-扩展运算符
- ES6 Rest参数
- es6 rest 参数
- ES6(ECMAScript 6 ) 解构 扩展运算符
- es6 扩展运算符 三个点(...)
- Pagerank的C++实现原理
- mssqlserver xp_cmdshell提权
- 解决Unity引擎后期渲染的性能问题(转)
- 记录手动处理form表单中对table表格中的行数据的收集
- vmware虚拟机启动centOs黑屏
- ES6学习之路3----rest参数与扩展运算符
- 51Nod 1031 骨牌覆盖
- 小程序接口---json格式
- opencv中ArUco识别
- 深度学习:语言模型的评估标准
- 1024. 科学计数法 (20) PAT乙级真题
- ubuntu16.04 Git add 使用tab键卡死
- mac之tomcat安装启动配置
- 《算法艺术与信息学竞赛》之 递归与分治法 例四 丢失的数