当前位置:网站首页>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 ;
边栏推荐
- Bm11 list addition (II)
- QT & OpenGL lighting
- Cv5200 wireless WiFi communication module, wireless video image transmission, real-time wireless communication technology
- 【图像分割】基于方向谷形检测实现静脉纹路分割附MATLAB代码
- 关于白盒测试,这些技巧你得游刃有余~
- Youqilin system installation beyondcomare
- Pytorch:快速求得NxN矩阵的主对角线(diagonal)元素与非对角线元素
- R language text mining and natural language processing tutorial
- Remember a uniapp experience
- Libgdx learning road 02: draw game map with tiled
猜你喜欢

BM16 删除有序链表中重复的元素-II

BM14 链表的奇偶重排

6-20漏洞利用-proftpd测试

Fantasy 5 (ue5) game engine complete course 2022

UWB module realizes personnel precise positioning, ultra wideband pulse technology scheme, and real-time centimeter level positioning application

How to obtain data on mobile phones and web pages after the SCM data is uploaded to Alibaba cloud Internet of things platform?

CVPR19 - 调参干货《Bag of Tricks for Image Classification with Convolutional Neural Network》

Application of time series database in cigarette factory

4、 Interface requests data to update input information interactively

VAE:变分自编码器的理解与实现
随机推荐
Fundamentals of software testing and development | practical development of several tools in testing and development
TSDB and blockchain
Doxygen文档生成工具
Application of TSDB in civil aircraft industry
[image segmentation] vein segmentation based on directional valley detection with matlab code
Kali doesn't have an eth0 network card? What if you don't connect to the Internet
2022年暑假ACM热身练习3(详细)
[filter tracking] target tracking based on EKF, TDOA and frequency difference positioning with matlab code
How to use Qianqian listening sound effect plug-in (fierce Classic)
三类6种地图可视化软件测评,最好用的工具居然是它
Pointer learning of C language -- the consolidation of pointer knowledge and the relationship with functions, arrays and structures
Wechat solves the problem of long press selected style
BM16 delete duplicate elements in the ordered linked list -ii
How does the mqtt server built with emqx forward data and save it to the cloud database?
Structure and working principle of thyristor
uwb模块实现人员精确定位,超宽带脉冲技术方案,实时厘米级定位应用
Libgdx learning path 01: libgdx introduction and running tutorial
Self cultivation of Electronic Engineers - when a project is developed
Qt: one signal binds multiple slots
ICLR21(classification) - 未来经典“ViT” 《AN IMAGE IS WORTH 16X16 WORDS》(含代码分析)