当前位置:网站首页>JS arrow function user and processing method of converting arrow function into ordinary function

JS arrow function user and processing method of converting arrow function into ordinary function

2022-04-23 20:25:00 First code

1 Arrow function

The arrow function is ES6 New functional forms appear

namely , Use ES6 Arrow function Syntax definition function , The original function of the “function” Delete the keyword and function name , And use “=>” Connecting parameter lists and function bodies .

Such as : Definition of ordinary function
 

var fn1 = function(a, b) {
    return a + b
}
 
function fn2(a, b) {
    return a + b
}

Arrow function definition , by

var fn1 = (a, b) => {
    return a + b
}
 
(a, b) => {
    return a + b
}

Detailed instructions , Reference resources :ES6 New feature arrow function syntax 、 How to use arrow function correctly

2 Arrow function to ordinary function

Arrow function

this.$get('/posts').then(res=>{
      console.log(this)
      this.datalist=res.data
    })

Convert to ordinary function : Recuperation function(),res Just as one of the parameters

this.$get('/posts').then(function(res){
      console.log(this)
    })

3 Particular attention This The direction of

Ordinary function : Which object calls a normal function , Ordinary functions point to this object .
Arrow function : Arrowhead function this Is the upper level object of inheritance .

Two of the above code this Object pointing is different , Specifically, you can try .

Reference resources :

JS The difference between ordinary function and arrow function

ES6 New feature arrow function syntax 、 How to use arrow function correctly

 

版权声明
本文为[First code]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210551267601.html