当前位置:网站首页>Function default parameters, arrow functions, and remaining parameters in ES6 - explanation
Function default parameters, arrow functions, and remaining parameters in ES6 - explanation
2022-07-26 08:08:00 【JackieDYH】
1. Function default parameters , When calling, the last parameter can be executed without passing the corresponding argument
function show(a=" ha-ha ",b=" Hello ") {
console.log(a,b);
}
show(' La! ',);// La! Hello
function show2({x,y=0}) {
console.log(x,y)
}
show2({x:1});//1,0
2. The parameter name of the function has been defined by default , No more let,const Repeat the definition
function show3(a=18) {
let a=100;
console.log(a)
}
show3();// Report errors Identifier 'a' has already been declared
3. Three points ... Extension operator 、rest Operator , Remainder operator , Can be applied to expand arrays Reset array Sort
let arr=['apple','banana','orange'];
console.log(...arr);//apple banana orange
// Expand array
function show4(...a) {
console.log(a);
}
show4(1,2,3,4,5);//[1, 2, 3, 4, 5]
// Reset array
function show4(...arr1) {
//let arr1=Array.prototype.slice.call(arguments);
return arr1.sort();
}
console.log(show4(10,2,111,4,50));// Sort by the first number . Non size
// Sort
function show5(a,b,c) {
console.log(a,b,c);
}
show5(...[1,9,8]);//1 9 8
function show6(a,b,...c) {
console.log(a,b);
console.log(c)
}
show6(1,2,3,4,5);//1 2 //[3,4,5]
// Receive the remaining parameters as an array
let arr3=[1,2,3,4,5],
arr4=[...arr3];
// Equate to
let arr5=Array.from(arr3);
console.log(arr3,arr4,arr5);
// Copy the array
let str='a-b-c';
let arr6=Array.from(str);
console.log(arr6);//["a", "-", "b", "-", "c"];
// String into an array of single characters
4. Arrow function
function show7() {
return 1;
}
// Equate to
let show8=()=>1;
console.log(show7(),show8()); //1 1
let show9=(d=8,f=12)=>{
console.log(d,f);
return d+f;
};
show9(5,12);
console.log(show9(5,12));//17
Format 1:()=>return Things that are , Not very practical
Format 2:
()=>{
sentence ;
return;
}var id=10;//var Define a variable that belongs to window
let json={
id:1,
show:function() {
setTimeout(()=>{
alert(this.id);// Who is calling this,this Whose is it? . namely this To define the object where the function is located , Not the object at runtime .
}//1 instead of 10
,2000);
alert(this.id);
}
};
json.show();
(1) When you use the arrow function ,this Does not belong to the current function , It belongs to the scope of the currently defined object .
(2) There is no arguments, use ... solve
(3) Arrow functions cannot be constructors
let show10=(...args)=>{
console.log(args);
};
show10(1,2,3,4,5);
let show11=()=>{
this.name='abc';
};
let s=new show11();
alert(s.name);// Report errors
ajax(url,(res)=>{
// The current function does not have this Scope
})
es5:this Who is calling this Who belongs to , Arrowhead function this Is the object where the function is defined .
边栏推荐
- Burp Suite - Chapter 1 burp suite installation and environment configuration
- R language foundation
- The difference between throw and throws?
- Matlab drawing black spots on two / three-dimensional drawings
- 一键部署LAMP和LNMP架构
- Zroi easy sum (generating function, block, DP, combination, polynomial)
- How to determine the authenticity of the website you visit -- certificate system
- Add traceid to the project log
- Use js to count the number of occurrences of each string in the string array, and format it into an object array.
- Lambda and stream
猜你喜欢
![[fastjson1.2.24 deserialization vulnerability principle code analysis]](/img/14/8f6a75fe5f06c19eeff9c7204979c3.png)
[fastjson1.2.24 deserialization vulnerability principle code analysis]

Software engineering -- dental clinic -- demand acquisition
![[uniapp] encapsulation of multiple payment methods](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[uniapp] encapsulation of multiple payment methods
![[xshell7 free download and installation]](/img/1f/7ac3e2c40c1b3ef2e7ce7403541972.png)
[xshell7 free download and installation]

QT listview add controls and pictures

Burp suite Chapter 9 how to use burp repeater

Crawler - > tpimgspider

Now developers are beginning to do testing. Will there be no software testers in the future?

我,35岁了。

"Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
随机推荐
这是一张图片
The difference between FileInputStream and bufferedinputstream
2W word detailed data Lake: concept, characteristics, architecture and cases
万字长文 | 深入理解 OpenFeign 的架构原理
Reading and writing properties file
全网最全:Mysql六种约束详解
Dev gridcontrol 捕获按键事件
[fastjson1.2.24 deserialization vulnerability principle code analysis]
Leetcode sword finger offer special (I) integer
2022 / 7 / 16 exam summary
Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment
Excel file reading and writing (creation and parsing)
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
Why is Google's internal tools not suitable for you?
Idea settings set shortcut keys to convert English letters to case in strings
Basic introduction of JDBC
《门锁》引爆独居安全热议 全新海报画面令人窒息
Rewriting and overloading
JSP action -- usebean action