当前位置:网站首页>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 })
边栏推荐
- C语言提高篇(一)
- 试着换个角度理解低代码平台设计的本质
- MySQL high performance optimization notes (including 578 pages of notes PDF document), collected
- Jetpack -- navigation realizes page Jump
- ResNet50+k折交叉验证+数据增强+画图(准确率、召回率、F值)
- 弹性布局 单选
- Ciscn 2022 central China Misc
- [circuit design] open collector OC output of triode
- Try to understand the essence of low code platform design from another angle
- How to find the right agent type? Multi angle analysis for you!
猜你喜欢

MySQL high performance optimization notes (including 578 pages of notes PDF document), collected
![[one · data | chained binary tree]](/img/83/d62a47f1264673f1e898335303a7a6.png)
[one · data | chained binary tree]

Anti crawler mechanism solution: JS code generates random strings locally
[electronic components] zener diode

数学建模——带相变材料的低温防护服御寒仿真模拟

druid. IO custom real-time task scheduling policy

Establish an engineering template based on STM32 in keil -- detailed steps

Navigation--实现Fragment之间数据传递和数据共享

Have you ever encountered the situation that the IP is blocked when crawling web pages?

autoware中ndtmatching功能加载点云图坐标系修正的问题
随机推荐
Understand the working principle of timer in STM32 in simple terms
Web crawler API Quick Start Guide
Jetpack -- understand the use of ViewModel and livedata
autoware中ndtmatching功能加载点云图坐标系修正的问题
2022.7.28-----leetcode.1331
数学建模——带相变材料的低温防护服御寒仿真模拟
How to prevent all kinds of affiliated fraud?
Try to understand the essence of low code platform design from another angle
“蔚来杯“2022牛客暑期多校训练营3,签到题CAJHF
[circuit design] convert AC AC to DC
Leetcode exercise - Sword finger offer 45. arrange the array into the smallest number
Flexible layout single selection
什么是作用域和作用域链
Idea connection database
Blind separation of speech signals based on ICA and DL
Force deduction brush question (2): sum of three numbers
Control buzzer based on C51
"Activity recommendation" rush rush! 2022 international open source Festival has new content
Mathematical modeling -- heat conduction of subgrade on Permafrost
leetcode/和为k的连续子数组个数