<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          ES6-箭頭函式

          共 2787字,需瀏覽 6分鐘

           ·

          2022-05-09 00:13

          ddfb2877cb3d247de13347ea69cd62c6.webp

          來(lái)源 |?https://eyesofkids.gitbooks.io/javascript-start-from-es6/content/part4/arrow_function.html


          箭頭函式(Arrow Functions)是ES6標(biāo)準(zhǔn)中,最受歡迎的一種新語(yǔ)法。它會(huì)受歡迎的原因是好處多多,而且沒(méi)有什么副作用或壞處,只要注意在某些情況下不要使用過(guò)頭就行了。有什么好處呢?大致上有以下幾點(diǎn):

          • 語(yǔ)法簡(jiǎn)單。

          • 可以讓程式碼的可閱讀性提高。

          • 某些情況下可以綁定this值。

          語(yǔ)法

          箭頭函式的語(yǔ)法如下,出自箭頭函數(shù)(MDN):

          ([param] [, param]) => {   statements}
          param => expression

          簡(jiǎn)單的說(shuō)明如下:

          • 符號(hào)是肥箭頭(=>) (注: "->"是瘦箭頭)

          • 基本特性是"函式表達(dá)式(FE)的簡(jiǎn)短寫法"

          一個(gè)簡(jiǎn)單的范例是:

          const func = (x) => x + 1

          相當(dāng)于

          const func = function (x) { return x + 1 }

          所以你可以少打很多英文字元與一些標(biāo)點(diǎn)符號(hào)之類的,所有的函式會(huì)變成匿名的函式?;旧系姆?hào)使用如下說(shuō)明:

          • 花括號(hào)({})是有意義的,如果函式?jīng)]回傳東西就要花括號(hào)。例如()=>{}

          • 只有單一個(gè)傳入?yún)?shù)時(shí),括號(hào)(())可以不用,例如x=>x*x

          最容易搞混的是下面這個(gè)例子,有花括號(hào)({})與沒(méi)有是兩碼子事:

          const funcA = x => x + 1const funcB = x => { x + 1 }
          funcA(1) //2funcB(1) //undefined

          當(dāng)沒(méi)用花括號(hào)({})時(shí),代表會(huì)自動(dòng)加return,也只能在一行的語(yǔ)句的時(shí)候使用。使用花括號(hào)({})則是可以加入多行的語(yǔ)句,不過(guò)return不會(huì)自動(dòng)加,有需要你要自己加上,沒(méi)加這個(gè)函式最后等于return undefined(注: 這是JavaScript語(yǔ)言中函式的設(shè)計(jì))。

          綁定this值

          箭頭函式可以取代原有使用var self = this或.bind(this)的情況,它可以在詞匯上綁定this變數(shù)。這在特性篇"this"章的內(nèi)容中有提到。但有時(shí)候情況會(huì)比較特殊,要視情況而定,而不是每種情況都一定可以用箭頭函式來(lái)取代。

          原本的范例:

          const obj = {a:1}
          function func(){ const that = this
          setTimeout( function(){ console.log(that) }, 2000)}
          func.call(obj) //Object {a: 1}

          可以改用箭頭函式:

          const obj = {a:1}
          function func(){ setTimeout( () => { console.log(this) }, 2000)}
          func.call(obj)

          用bind方法的來(lái)回傳一個(gè)部份函式的語(yǔ)法,也可以用箭頭函式來(lái)取代,范例出自Arrow functions vs. bind()?:

          function add(x, y) {       return x + y   }
          const plus1 = add.bind(undefined, 1)
          箭頭函式的寫法如下:
          const plus1 = y => add(1, y)

          不可使用箭頭函式的情況

          以下這幾個(gè)范例都是與this值有關(guān),所以如果你的箭頭函式里有用到this值要特別小心。以下的范例都只能用一般的函式定義方式,"不可"使用箭頭函式。

          使用字面文字定義物件時(shí),其中的方法

          箭頭函式會(huì)以定義當(dāng)下的this值為this值,也就是window物件(或是在嚴(yán)格模式的undefined),所以是存取不到物件中的this.array值的。

          const calculate = {  array: [1, 2, 3],  sum: () => {    return this.array.reduce((result, item) => result + item)  }}
          //TypeError: Cannot read property 'array' of undefinedcalculate.sum()

          物件的prototype屬性中定義方法時(shí)

          這種情況也是像上面的類似,箭頭函式的this值,也就是window物件(或是在嚴(yán)格模式的undefined)。

          function MyCat(name) {  this.catName = name}
          MyCat.prototype.sayCatName = () => { return this.catName}
          cat = new MyCat('Mew')// ReferenceError: cat is not definedcat.sayCatName()

          dom事件處理的監(jiān)聽者(事件處理函式)

          箭頭函式的this值,也就是window物件(或是在嚴(yán)格模式的undefined)。這里的this值如果用一般函式定義的寫法,應(yīng)該就是DOM元素本身,才是正確的值。

          const button = document.getElementById('myButton')
          button.addEventListener('click', () => { this.innerhtml = 'Clicked button'})//TypeError: Cannot set property 'innerHTML' of undefined

          建構(gòu)函式

          這會(huì)直接在用new運(yùn)算符時(shí)拋出例外,根本不能用。

          const Message = (text) => {  this.text = text;}// Throws "TypeError: Message is not a constructor"const helloMessage = new Message('Hello World!');

          其他限制或陷阱

          • 函式物件中的call、apply、bind三個(gè)方法,無(wú)法"覆蓋"箭頭函式中的this值。

          • 箭頭函式無(wú)法用于建構(gòu)式(constructor),使用new會(huì)產(chǎn)生錯(cuò)誤。(上面有范例)

          • 箭頭函式?jīng)]有一般函式有的隱藏arguments物件。

          • 箭頭函式不能當(dāng)作generators使用,使用yield會(huì)產(chǎn)生錯(cuò)誤。

          • 箭頭函式用于解決一般的this問(wèn)題是可以,但并不適用于全部的情況,尤其是在像jquery、underscore之類有callback(回調(diào))之類的api時(shí),有可能不是如預(yù)期般的結(jié)果。



          學(xué)習(xí)更多技能

          請(qǐng)點(diǎn)擊下方公眾號(hào)

          c3ad25a1de01dfc7b83780da46887476.webp

          85abe585e4f03532352c41d8f798bdf5.webp

          瀏覽 34
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  丁香激情婷婷午夜版 | 日批视频免费观看 | 日皮视频在线免费观看 | 豆花视频免费观看 | 伊人影院av |