当前位置:网站首页>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 .
边栏推荐
- 线程崩了,为什么不会导致 JVM 崩溃呢?如果是主线程呢?
- Burp suite Chapter 5 how to use burp target
- Now developers are beginning to do testing. Will there be no software testers in the future?
- The difference between FileInputStream and bufferedinputstream
- Template summary
- The difference between overloading and rewriting
- Utils connection pool
- The difference between throw and throws?
- Abstract classes and interfaces
- Exam summary on June 27, 2022
猜你喜欢

JSP implicit object servlet object

Burp suite Chapter 3 how to use burp suite agent

JSP action -- usebean action

CentOS install mysql5.7

小组成员参加2022中国多媒体大会

《门锁》引爆独居安全热议 全新海报画面令人窒息

2022-07-13 group 5 Gu Xiangquan's learning notes day06

Team members participate in 2022 China multimedia conference

QT listview add controls and pictures
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
随机推荐
Burp Suite-第三章 如何使用Burp Suite代理
Stm8 official library file download
NFS service and Samba service deployment
Introduction to C language (8)
Common database commands (special for review)
Excel file reading and writing (creation and parsing)
Exam summary on June 30, 2022
全网最全:Mysql六种约束详解
Common methods of string: construction method, other methods
Software engineering -- dental clinic -- demand acquisition
Enterprise private network construction and operation and maintenance
2022/7/1
2022/7/11 exam summary
【 fastjson1.2.24反序列化漏洞原理代码分析】
2022-07-13 group 5 Gu Xiangquan's learning notes day06
线程崩了,为什么不会导致 JVM 崩溃呢?如果是主线程呢?
Introduction to arrays -- array
C# 获取选择文件信息
吉他五线谱联系 茉莉花
Now developers are beginning to do testing. Will there be no software testers in the future?