日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
=> 箭頭函數(shù) · 方便、快捷,也要小心坑

箭頭函數(shù)是必須要掌握的,今天我們一起來學(xué)習(xí)一下,它給開發(fā)者帶來方便的同時(shí),也要留意它的「無能」。先看一個(gè)例子:

站在用戶的角度思考問題,與客戶深入溝通,找到鉛山網(wǎng)站設(shè)計(jì)與鉛山網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、主機(jī)域名、虛擬空間、企業(yè)郵箱。業(yè)務(wù)覆蓋鉛山地區(qū)。

 
 
 
 
  1. const names = [ 
  2.     'wsy', 
  3.     'suyan', 
  4.     '前端小課' 
  5. ]; 
  6. let lengths = names.map(name => name.length); 
  7. console.log('lengths = ', lengths); 

結(jié)果如圖:

先看下它的語法:

1. 無參數(shù)

 
 
 
 
  1. function call(callback) { 
  2.     callback(); 
  3. call(() => { 
  4.     console.log('arrow void'); 
  5. }); 
  6. // 箭頭函數(shù)類似于下面這個(gè)函數(shù) 
  7. call(function () { 
  8.     console.log('void'); 
  9. }); 

2. 只有一個(gè)參數(shù),無返回值

 
 
 
 
  1. function call(callback) { 
  2.     callback('前端小課'); 
  3. call(name => { 
  4.     console.log('arrow', name); 
  5. }); 
  6. // 箭頭函數(shù)類似于下面這個(gè)函數(shù) 
  7. call(function (name) { 
  8.     console.log(name); 
  9. }); 

3. 只有一個(gè)參數(shù),有返回值

 
 
 
 
  1. function call(callback) { 
  2.     // 返回值為 4 
  3.     let len = callback('前端小課'); 
  4.     console.log(len); 
  5.  
  6. // 只有一行表達(dá)式省略大括號(hào) 
  7. call(name => name.length); 
  8. // 類似于這個(gè) 
  9. call(name => { 
  10.     return name.length; 
  11. }); 
  12. // 箭頭函數(shù)類似于下面這個(gè)函數(shù) 
  13. call(function (name) { 
  14.     return name.length; 
  15. }); 

4.有多個(gè)參數(shù),有返回值

 
 
 
 
  1. function call(callback) { 
  2.     let len = callback(1, 2, 3); 
  3.     console.log(len); // 6 
  4.  
  5. // 多個(gè)個(gè)參數(shù),有返回值,只有一行表達(dá)式省略大括號(hào) 
  6. call((a, b, c) => a + b + c); 
  7. // 類似與這個(gè) 
  8. call((a, b, c) => { 
  9.     return a + b + c; 
  10. }); 
  11. // 箭頭函數(shù)類似于下面這個(gè)函數(shù) 
  12. call(function (a, b, c) { 
  13.     return a + b + c; 
  14. }); 

從上面這些例子可以知道,每個(gè)箭頭函數(shù)都能寫出一個(gè)與其功能相同的普通函數(shù),那為什么還用箭頭函數(shù)?

在 連接你、我、他 —— this 這節(jié)課程中使用箭頭函數(shù)解決了 this 指向的問題。不知道你們有沒有發(fā)現(xiàn)當(dāng)寫下面這兩個(gè)函數(shù)的時(shí)候,VSCode 默認(rèn)使用的是箭頭函數(shù):

 
 
 
 
  1. setTimeout(() => { 
  2.     // 這里是箭頭函數(shù) 
  3. }, 100); 
  4.  
  5. setInterval(() => { 
  6.     // 這個(gè)是箭頭函數(shù) 
  7. }, 200); 

使用箭頭函數(shù)有幾點(diǎn)需要留意:

1. 讓函數(shù)更簡短,上面例 3 就是一個(gè)很好的例子;

2. 沒有自己的 this 和 argument,比如有如下代碼:

 
 
 
 
  1. let person = { 
  2.     name: 'suyan', 
  3.     showName: function (age) { 
  4.         window.setTimeout(() => { 
  5.             console.log('this = ', this); 
  6.             console.log('arguments = ', arguments); 
  7.             console.log(this.name, age); 
  8.         }, 100); 
  9.     } 
  10. }; 
  11. person.showName(20); 

打印結(jié)果為:

3. 不能作為構(gòu)造函數(shù);

 
 
 
 
  1. let Dog = name => { 
  2.     this.name = name; 
  3. }; 
  4. // 錯(cuò)誤 Uncaught TypeError: Dog is not a constructor 
  5. let aDog = new Dog('fe'); 
  6.  
  7. let Dog2 = function (name) { 
  8.     this.name = name; 
  9. }; 
  10. // 正確 
  11. let aDog2 = new Dog2('fe'); 

4. 箭頭函數(shù)沒有 prototype 屬性:

 
 
 
 
  1. let Dog = name => { 
  2.     this.name = name; 
  3. }; 
  4. Dog.prototype; // undefined 

5.不能通過 call、apply 綁定 this

 
 
 
 
  1. var name = 'I am widow'; 
  2.  
  3. let animal = { 
  4.     name: 'animal', 
  5.     showName: age => { 
  6.         console.log('this = ', this); 
  7.         console.log('name | age = ', this.name, age); 
  8.     } 
  9. }; 
  10. let dog = { 
  11.     name: 'dog' 
  12. }; 
  13.  
  14. animal.showName.call(dog, 20); 
  15. animal.showName.apply(dog, [21]); 
  16. let bindName = animal.showName.bind(dog, 22); 
  17. bindName(); 

運(yùn)行代碼,結(jié)果如下:

由于箭頭函數(shù)沒有 this 指針,不能通過 call、apply、bind 的方式來修改 this。只能傳遞參數(shù),this 參數(shù)將被忽略。


網(wǎng)站標(biāo)題:=> 箭頭函數(shù) · 方便、快捷,也要小心坑
網(wǎng)頁地址:http://m.5511xx.com/article/cojdpep.html