当前位置:网站首页>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 .
边栏推荐
- 2022.7.22DAY612
- 2022 / 7 / 16 exam summary
- Distributed system and distributed database system (Introduction)
- es6中函数默认参数、箭头函数、剩余参数-讲解
- Summary of distributed related interview questions
- Template summary
- C# WinForm中PreviewKeyDown、KeyDown、KeyPress、KeyUp区别与联系
- Burp suite Chapter 5 how to use burp target
- Exam summary on July 15, 2022
- 全网最全:Mysql六种约束详解
猜你喜欢

Lambda and stream

吉他五线谱联系 茉莉花

SPSS uses kmeans, two-stage clustering and RFM model to study the behavior law data of borrowers and lenders in P2P network finance

Software engineering -- dental clinic -- demand acquisition

The first ide overlord in the universe, replaced...

"Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating

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

Database foundation

数组的介绍--Array

Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
随机推荐
Establishment and use of openstack cloud platform
[uniapp] encapsulation of multiple payment methods
2022.7.22DAY612
Dev gridcontrol 捕获按键事件
2022.7.22DAY612
Logical volume management (LVM)
宇宙第一 IDE 霸主,换人了。。。
2022/7/11 exam summary
Let's talk about the three core issues of concurrent programming.
IDEA settings设置快捷键实现字符串中的英文字母转大小写
The difference between overloading and rewriting
Crawler - > tpimgspider
Burp suite Chapter 9 how to use burp repeater
Brief introduction to XML
Software engineering -- dental clinic -- demand analysis
Table fix specific rows
万字长文 | 深入理解 OpenFeign 的架构原理
Exam summary on June 30, 2022
Software engineering -- dental clinic -- demand acquisition
Stm8 official library file download