当前位置:网站首页>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 })
边栏推荐
- 自定义mvc原理和框架实现
- Control the pop-up window and no pop-up window of the input box
- [circuit design] open collector OC output of triode
- Promise解决异步
- [one · data | chained binary tree]
- Lxml web page capture the most complete strategy
- [cloud native] what is the microservice architecture
- Opencv image sharpness evaluation (camera autofocus)
- Related function records about string processing (long-term update)
- [simple implementation and extension of one · data | array heap]
猜你喜欢

Internet of things development -- mqtt message server emqx

Monadic linear function perceptron: Rosenblatt perceptron

LM13丨形态量化-动量周期分析

Cookies and sessions

Read the recent trends of okaleido tiger and tap the value and potential behind it

Detailed explanation of IVX low code platform series -- Overview (II)

Force deduction brush question (2): sum of three numbers
![[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)](/img/07/d5861404a76a0fb7d6d5f930a4a66a.png)
[electronic components] constant voltage, amplify the current of the load (triode knowledge summary)

TI C6000 TMS320C6678 DSP+ Zynq-7045的PS + PL异构多核案例开发手册(2)
![[one · data | chained binary tree]](/img/83/d62a47f1264673f1e898335303a7a6.png)
[one · data | chained binary tree]
随机推荐
Understand the working principle of timer in STM32 in simple terms
Mathematical modeling - location of police stations
(CVPR-2019)选择性的内核网络
H5 background music is played automatically by touch
Leetcode/0 and 1 consecutive subarrays with the same number
[simple implementation and extension of one · data | array heap]
向量相似度评估方法
Establish an engineering template based on STM32 in keil -- detailed steps
Opencv image sharpness evaluation (camera autofocus)
点击回到顶部js
基于C51控制蜂鸣器
How to crawl web pages with playwright?
2022.7.27-----leetcode.592
druid. IO custom real-time task scheduling policy
Click back to the top JS
Semiconductor chip industry chain
Cookies and sessions
JS dom2 and dom3
leetcode/0和1个数相同的连续子数组
Control buzzer based on C51