当前位置:网站首页>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 })
边栏推荐
- Cookies and sessions
- H5 background music is played automatically by touch
- 解决使用ESlint时,和vetur冲突导致保存变双引号,结尾逗号等
- In 2022, the official data of programming language ranking came, which was an eye opener
- Jetpack -- understand the use of ViewModel and livedata
- Blind separation of speech signals based on ICA and DL
- Comprehensive explanation of "search engine crawl"
- 弹性布局 单选
- FPGA实现10M多功能信号发生器
- How to crawl web pages with playwright?
猜你喜欢

How to find the right agent type? Multi angle analysis for you!

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

Solution of Lenovo notebook camera unable to open

Navigation -- realize data transmission and data sharing between fragments

Comprehensive explanation of "search engine crawl"

年中总结 | 与自己对话,活在当下,每走一步都算数

E-commerce keyword research helps data collection

Comprehensive use method of C treeview control

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

Have you ever encountered the situation that the IP is blocked when crawling web pages?
随机推荐
[MySQL] SQL aliases the table
The growth path of embedded engineers
Using local cache + global cache to realize user rights management of small systems
Jetpack -- understand the use of ViewModel and livedata
Promise解决异步
How to crawl web pages with playwright?
Leetcode 242. valid anagram
年中总结 | 与自己对话,活在当下,每走一步都算数
Promise solves asynchrony
点击回到顶部js
Solution of Lenovo notebook camera unable to open
[simple implementation and extension of one · data | array heap]
Navigation -- realize data transmission and data sharing between fragments
FPGA实现10M多功能信号发生器
Complete collection of common error handling in MySQL installation
Read the recent trends of okaleido tiger and tap the value and potential behind it
Jetpack--了解ViewModel和LiveData的使用
Why can't Bi software do correlation analysis
Mathematical modeling -- the laying of water pipes
Semiconductor chip industry chain