当前位置:网站首页>ES6 new - arrow function
ES6 new - arrow function
2022-07-28 19:23:00 【ca11meback】
Arrow functions provide a more concise way to write functions . The basic grammar is :
Parameters => The body of the function
( Parameters ) => { The body of the function }
var fn = (x, y) => {
console.log(this)
return x + y
}This is our ES6 New arrow function
You can see from the above that the constructor is missing function, Then an arrow points to the code block of our function
!!! Special attention should be paid to the following situation if our parameter has only one !!!
fn=x=》{}
There is no need to write parentheses for parameters
!!! If the execution body of the arrow function has only one expression Is the return value Then you can omit the braces !!!
var fn2=(a,b)=>a*b
Then we will discuss the greater difference between arrow function and general function
It's the arrow function this problem
var obj={
name:"karen",
say:function(){
var fn=(a)=>{
console.log(this,1111)
}
var fm=function(){
console.log(this,2222)
}
fm()
fn(2)//fn Who is the caller of the function window
}
}
obj.say()// caller obj
What you can see is our arrow function , One is that we normally construct early functions , And the function is basically similar internally, but why this The objects are different
function tool(ad){
ad()
}
var obj={
name:"bob",
say:function(){
console.log(this.name)
},
makeMoney:function(){
// var that=this;
tool(function(){
console.log(this.name+" Eat the rice ",111)
})
// tool(()=>{
// console.log(this.name+" Eat the rice ",111)
// })
}
}
obj.makeMoney()
// Eat the rice 111We can know from the results that the desired effect did not appear ,this.name It doesn't work because the callback function we use The function is specifically run in the second line of our code , therefore this The object is Windows, use es5 The way to solve this problem is to use a variable outside this function to save the current environment , Then use in the code
but ES6 Arrow function in this It's directly pointing to this obj object Why? ?
reason : Arrow in function body this object , Is the object when the function is defined , Instead of the object when using the function .
This also leads to a problem !
We can't use arrow functions to construct objects because this The problem of
The arrow function doesn't argument attribute , What is used to save the number of arguments ?
Use ... To accept : There's nothing in the arrow function arguments, have access to …reset, The received array type , All arguments except formal parameters are received
for instance
var show = (a, b, ...reset) => {
console.log(a + b);
console.log(reset);
}
show(1, 2, 3, 4, 5);The arrow function sums up
Have an arrow
The arrow is preceded by parentheses , Put formal parameters , Parentheses can be omitted when there is only one formal parameter ;
After the arrow is the function body ;
If the function body has only one statement , No, {}, The return value at this time does not need return;
Inside the arrow function this Always point to the nearest function Inside this;
Methods in objects , Try not to use arrow functions ;
There's nothing in the arrow function arguments, have access to …reset, The received array type , All arguments except formal parameters are received ;
边栏推荐
- 3、 Uni app fixed or direct to a certain page
- The ever-changing pointer ----- C language
- Getting started with QT & OpenGL
- UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application
- Application of time series database in Hydropower Station
- Validate hardware DDR design with Xilinx MIG
- R语言与数据分析实战11-数据的删除
- ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)
- Adobe XD web design tutorial
- 架构实战营第8模块作业
猜你喜欢

DevCon.exe 导出output至指定文件

剑指 Offer II 109. 开密码锁

【物理应用】大气吸收损耗附matlab代码

VAE:变分自编码器的理解与实现

当CNN遇见Transformer《CMT:Convolutional Neural Networks Meet Vision Transformers》

Validate hardware DDR design with Xilinx MIG

Pytorch:交叉熵损失(CrossEntropyLoss)以及标签平滑(LabelSmoothing)的实现

VIM learning manual

Applet applet jump to official account page

Srs4.0 installation steps
随机推荐
Method of win7 system anti ARP attack
Structure and working principle of thyristor
UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application
PyG搭建异质图注意力网络HAN实现DBLP节点预测
How to use Qianqian listening sound effect plug-in (fierce Classic)
2、 Uni app login function page Jump
unity CS1513
SQL audit tool self introduction owls
Fundamentals of software testing and development | practical development of several tools in testing and development
Validate hardware DDR design with Xilinx MIG
Image processing web application development tutorial
[data analysis] realize SVDD decision boundary visualization based on MATLAB
图书管理数据库系统设计
Application of time series database in Hydropower Station
QT & OpenGL lighting
Pytorch GPU yolov5 reports an error
[image segmentation] vein segmentation based on directional valley detection with matlab code
[radar] radar signal online sorting based on kernel clustering with matlab code
Qt: one signal binds multiple slots
剑指 Offer II 109. 开密码锁