当前位置:网站首页>ES6 语法扩展
ES6 语法扩展
2022-07-29 01:33:00 【weixin_47254122】
撰写时间:2022 年 7 月 27日
ES6 语法扩展
剩余参数:
认识剩余参数:
const add=(x,y,z,…arg)=>{};
剩余参数的本质===>剩余参数永远是个数组,即使没有值也是一个空数组
const add = (x, y, …arg) => {
console.log(x, y, arg);
}
add(2, 3, 5, 6);// x=2 y=3 arg=[5,6]
剩余参数的默认值
1.箭头函数的剩余参数
箭头函数的参数部分即使只有一个剩余参数,也不能省略圆括号:
const add=(…arg)=>{}
2.使用剩余参数代替arguments获取实际的参数:
const add = function () {
// arguments 是一个类数组对象
console.log(arguments);
}
add(1, 2);
const add = (…arg) => {
// 箭头函数中没有arguments对象
// 可以使用剩余参数arg代替arguments
console.log(arg);// arg是一个数组
}
add(1, 2);
3.剩余参数的位置
剩余参数只能是最后一个参数,之后不能再有其他的参数,否则会报错:
const add=(x,y,z,…arg)=>{};
剩余参数的应用:
1.完成add函数
const add = (…arg) => {
let sum = 0;
for (let i = 0; i < array.length; i++) {
sum += arg[i];
}
return sum;
}
add(1, 2, 4, 4, 6, 7, 6)
2.与解构赋值结合使用
剩余参数不一定非要作为函数的参数使用
// 必须是最后一个参数
const [num, …args] = [2, 3, 5, 6, 7];
//const […args,num ] = [2, 3, 5, 6, 7];//报错
console.log(num, args);//args = [3,5,6,7]
// 剩余元素z结合解构对象 z也可以是一个对象
const { x, y, …z } = { x: 3, a: ‘a’, b: ‘b’, y: 2 };
console.log(z);//z={a: “a”,b: “b”}
//函数参数的解构
const func = ({ x, y, …z }) => {
console.log(z);//z={a: “a”,b: “b”}
}
func({ x: 3, a: ‘a’, b: ‘b’, y: 2 })
边栏推荐
- druid. io kill -9 index_ Realtime traceability task
- JVM内存溢出在线分析Dump文件以及在线分析打开.hprof文件得出JVM运行报告jvisualvm怎么在线分析
- Mathematical modeling -- the laying of water pipes
- 基于 ICA 与 DL 的语音信号盲分离
- LM13丨形态量化-动量周期分析
- druid. The performance of IO + tranquility real-time tasks is summarized with the help of 2020 double 11
- "Wei Lai Cup" 2022 Niuke summer multi school training camp 3, sign in question cajhf
- “蔚来杯“2022牛客暑期多校训练营2,签到题GJK
- 数学建模——派出所选址
- Click the button to slide to the specified position
猜你喜欢

druid. io kill -9 index_ Realtime traceability task

基于C51实现数码管的显示

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)

Blind separation of speech signals based on ICA and DL

自定义mvc原理和框架实现
![[circuit design] peak voltage and surge current](/img/d5/45bf9a79171ff9b8d7ba4c771b340c.png)
[circuit design] peak voltage and surge current

E-commerce keyword research helps data collection

MySQL安装常见报错处理大全

试着换个角度理解低代码平台设计的本质

Motionlayout -- realize animation in visual editor
随机推荐
Rgbd point cloud down sampling
字符流综合练习解题过程
Resolve the conflict with vetur when using eslint, resulting in double quotation marks and comma at the end of saving
MySQL high performance optimization notes (including 578 pages of notes PDF document), collected
mobile-picker.js
druid. io kill -9 index_ Realtime traceability task
弹性布局 单选
What is a proxy server? [2022 guide]
Detailed explanation of IVX low code platform series -- Overview (II)
IDEA 连接 数据库
RGBD点云降采样
MotionLayout--在可视化编辑器中实现动画
Force deduction brush question (1): sum of two numbers
【ONE·Data || 数组堆简单实现及其延伸】
Understand the working principle of timer in STM32 in simple terms
[circuit design] peak voltage and surge current
Add graceful annotations to latex formula; "Data science" interview questions collection of RI Gai; College Students' computer self-study guide; Personal firewall; Cutting edge materials / papers | sh
[云原生]微服务架构是什么
12. < tag dynamic programming and subsequence, subarray> lt.72. edit distance
年中总结 | 与自己对话,活在当下,每走一步都算数