当前位置:网站首页>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 .
边栏推荐
猜你喜欢

AQS implementation principle

Lambda and stream

Spotty music data client_ ID account

Why don't you tell me what long polling is?

Burp suite Chapter 7 how to use burp scanner

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

Excel file parsing

How to close the high-level port

A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment

How to determine the authenticity of the website you visit -- certificate system
随机推荐
What are the differences between FileInputStream and bufferedinputstream?
Burp Suite-第八章 如何使用Burp Intruder
Brief introduction to XML
shardingjdbc踩坑记录
Dev gridcontrol 捕获按键事件
BGP的基本配置
Ten thousand words long article | deeply understand the architecture principle of openfeign
How to close the high-level port
《门锁》引爆独居安全热议 全新海报画面令人窒息
Logical volume management (LVM)
利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。
2022/7/17 exam summary
给项目日志加上traceid
分享高压超低噪声LDO测试结果(High Voltage Ultra-low Noise LDO)
[June 29, 2022] examination summary
A clear summary and configuration example of GPON has been highlighted
FTP service
AQS implementation principle
Summary of common methods of string
Stm8 official library file download