当前位置:网站首页>ES6 syntax extension
ES6 syntax extension
2022-07-29 02:16:00 【weixin_ forty-seven million two hundred and fifty-four thousand】
Writing time :2022 year 7 month 27 Japan
ES6 Grammar extension
The remaining parameters :
Recognize the remaining parameters :
const add=(x,y,z,…arg)=>{};
Essence of residual parameters ===> The remaining parameters are always an array , Even if there is no value, it is an empty array
const add = (x, y, …arg) => {
console.log(x, y, arg);
}
add(2, 3, 5, 6);// x=2 y=3 arg=[5,6]
The default values of the remaining parameters
1. The remaining parameters of the arrow function
The parameter part of the arrow function, even if there is only one remaining parameter , You can't omit parentheses :
const add=(…arg)=>{}
2. Use the remaining parameters instead of arguments Get actual parameters :
const add = function () {
// arguments Is a class array object
console.log(arguments);
}
add(1, 2);
const add = (…arg) => {
// Not in arrow function arguments object
// You can use the remaining parameters arg Instead of arguments
console.log(arg);// arg Is an array
}
add(1, 2);
3. The location of the remaining parameters
The remaining parameter can only be the last parameter , After that, there can be no other parameters , Otherwise, an error will be reported :
const add=(x,y,z,…arg)=>{};
Application of remaining parameters :
1. complete add function
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. In combination with deconstruction assignment
The remaining parameters do not have to be used as parameters of the function
// Must be the last parameter
const [num, …args] = [2, 3, 5, 6, 7];
//const […args,num ] = [2, 3, 5, 6, 7];// Report errors
console.log(num, args);//args = [3,5,6,7]
// The remaining elements z Combine deconstruction objects z It can also be an object
const { x, y, …z } = { x: 3, a: ‘a’, b: ‘b’, y: 2 };
console.log(z);//z={a: “a”,b: “b”}
// Deconstruction of function parameters
const func = ({ x, y, …z }) => {
console.log(z);//z={a: “a”,b: “b”}
}
func({ x: 3, a: ‘a’, b: ‘b’, y: 2 })
边栏推荐
- 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
- What is the function of data parsing?
- Ciscn 2022 central China Misc
- Lxml web page capture the most complete strategy
- [cloud native] what is the microservice architecture
- [MySQL] SQL aliases the table
- Basic working principle and LTSpice simulation of 6T SRAM
- Introduction to shared data center agent
- 数学建模——带相变材料的低温防护服御寒仿真模拟
- Comprehensive analysis of news capture doorway
猜你喜欢

Force deduction brush question (2): sum of three numbers

一文读懂Okaleido Tiger近期动态,挖掘背后价值与潜力

MySQL stores JSON format data

Control buzzer based on C51

What is browser fingerprint recognition
![[cloud native] what is the microservice architecture](/img/84/a0ec68646083f3539aa39ad9d98749.png)
[cloud native] what is the microservice architecture

Solution of Lenovo notebook camera unable to open

Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number

Cookie和Session

字符流综合练习解题过程
随机推荐
[one · data | chained binary tree]
Number of consecutive subarrays with leetcode/ and K
In 2022, the official data of programming language ranking came, which was an eye opener
[cloud native and 5g] micro services support 5g core network
Form verification hidden input box is displayed before verification
[UE4] replay game playback for ue4.26
Force deduction brush question (1): sum of two numbers
How to crawl web pages with playwright?
数学建模——永冻土层上关于路基热传导问题
[云原生]微服务架构是什么
Mysql存储json格式数据
autoware中ndtmatching功能加载点云图坐标系修正的问题
The solution of reducing the sharpness of pictures after inserting into word documents
Ciscn 2022 central China Misc
"Wei Lai Cup" 2022 Niuke summer multi school training camp 2, sign in question GJK
iVX低代码平台系列详解 -- 概述篇(二)
Mobile communication -- simulation model of error control system based on convolutional code
Opencv image sharpness evaluation (camera autofocus)
Using local cache + global cache to realize user rights management of small systems
MySQL high performance optimization notes (including 578 pages of notes PDF document), collected