ES6箭頭函數(shù)如何使用

本篇內(nèi)容介紹了“ES6箭頭函數(shù)如何使用”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

創(chuàng)新互聯(lián)長期為1000多家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為靜海企業(yè)提供專業(yè)的成都網(wǎng)站建設(shè)、網(wǎng)站建設(shè),靜海網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

ES6箭頭函數(shù)如何使用

簡介

箭頭函數(shù)表達(dá)式的語法比函數(shù)表達(dá)式更簡潔,并且沒有自己的this,arguments,super或new.target。箭頭函數(shù)表達(dá)式更適用于那些本來需要匿名函數(shù)的地方,并且它不能用作構(gòu)造函數(shù)。

基礎(chǔ)用法

參數(shù)表示

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
//相當(dāng)于:(param1, param2, …, paramN) =>{ return expression; }

// 當(dāng)只有一個(gè)參數(shù)時(shí),圓括號(hào)是可選的:
(singleParam) => { statements }
singleParam => { statements }

// 沒有參數(shù)的函數(shù)應(yīng)該寫成一對(duì)圓括號(hào)。
() => { statements }

返回值表示

let add1 = (num1, num2) => {
  num1 + num2
};
let add2 = (num1, num2) => {
  return num1 + num2
};
let add3 = (num1, num2) => (num1 + num2);
let add4 = (num1, num2) => num1 + num2;

console.log(add1(2, 3));  // undefined
console.log(add2(2, 3)); // 5
console.log(add3(2, 3)); // 5
console.log(add4(2, 3)); // 5

進(jìn)階

//加括號(hào)的函數(shù)體返回對(duì)象字面量表達(dá)式:
let func = () => ({foo: 'bar'})
console.log(func()); // {foo: 'bar'}


//支持剩余參數(shù)和默認(rèn)參數(shù)
(param1, param2, ...rest) => { statements }
(param1 = defaultValue1, param2, …, paramN = defaultValueN) => {
statements }

//同樣支持參數(shù)列表解構(gòu)
let f = ([a, b] = [1, 2], {x: c} = {x: a + b}) => a + b + c;
f();  // 6

this

ES6箭頭函數(shù)如何使用

最佳實(shí)踐

  • 如果必須使用匿名函數(shù),或者inline 回調(diào)函數(shù),使用箭頭函數(shù)。eslint: prefer-arrow-callback, arrow-spacing

why?語法更簡潔,并且this更符合預(yù)期
如果函數(shù)邏輯相當(dāng)復(fù)雜,應(yīng)當(dāng)使用命名函數(shù)

// bad[1, 2, 3].map(function (x) {
  const y = x + 1;
  return x * y;});// good[1, 2, 3].map((x) => {
  const y = x + 1;
  return x * y;});
  • 如果函數(shù)體只有一條語句,且該語句不會(huì)產(chǎn)生副作用。使用簡寫方式,隱式返回;或者使用完整寫法,顯式return。
    eslint: arrow-parens, arrow-body-style

// bad
[1, 2, 3].map(number => {
  const nextNumber = number + 1;
  `A string containing the ${nextNumber}.`;
});

// good
[1, 2, 3].map(number => `A string containing the ${number}.`);

// good
[1, 2, 3].map((number) => {
  const nextNumber = number + 1;
  return `A string containing the ${nextNumber}.`;
});

// good
[1, 2, 3].map((number, index) => ({
  [index]: number,
}));

// No implicit return with side effects
function foo(callback) {
  const val = callback();
  if (val === true) {
    // Do something if callback returns true
  }
}

let bool = false;

// bad
foo(() => bool = true);

// good
foo(() => {
  bool = true;
});
  • 當(dāng)表達(dá)式占多行時(shí),使用括號(hào)括起來增強(qiáng)可讀性

why?函數(shù)開頭和結(jié)束更明確

// bad['get', 'post', 'put'].map(httpMethod => Object.prototype.hasOwnProperty.call(
    httpMagicObjectWithAVeryLongName,
    httpMethod,
  ));// good['get', 'post', 'put'].map(httpMethod => (
  Object.prototype.hasOwnProperty.call(
    httpMagicObjectWithAVeryLongName,
    httpMethod,
  )));
  • 如果函數(shù)只有一個(gè)參數(shù),省略括號(hào),省略花括號(hào)。否則,一直使用完整寫法,保持一致性。eslint: arrow-parens

// bad[1, 2, 3].map((x) => x * x);// good[1, 2, 3].map(x => x * x);// good[1, 2, 3].map(number => (
  `A long string with the ${number}. It’s so long that we don’t want it to take up space on the .map line!`));// bad[1, 2, 3].map(x => {
  const y = x + 1;
  return x * y;});// good[1, 2, 3].map((x) => {
  const y = x + 1;
  return x * y;});
  • 使用無歧義的=>語法,與<=,>=區(qū)分開。eslint: no-confusing-arrow

// badconst itemHeight = item => item.height > 256 ? item.largeSize : item.smallSize;// badconst itemHeight = (item) => item.height > 256 ? item.largeSize : item.smallSize;// goodconst itemHeight = item => (item.height > 256 ? item.largeSize : item.smallSize);// goodconst itemHeight = (item) => {
  const { height, largeSize, smallSize } = item;
  return height > 256 ? largeSize : smallSize;

簡單結(jié)論

  • 箭頭函數(shù)不能用new來創(chuàng)建構(gòu)造函數(shù)的實(shí)例,普通函數(shù)可以(因?yàn)榧^函數(shù)創(chuàng)建的時(shí)候程序不會(huì)為它創(chuàng)建construct方法,也就是沒有構(gòu)造能力,用完就丟掉了,不像普通函數(shù)重復(fù)利用,因此也不需要構(gòu)造函數(shù)原型,也就是不會(huì)自動(dòng)生成prototype屬性)

  • 程序不會(huì)給箭頭函數(shù)創(chuàng)建arguments對(duì)象

  • 普通函數(shù)中的this是動(dòng)態(tài)的,而箭頭函數(shù)中的this指向的是緊緊包裹箭頭函數(shù)的那個(gè)對(duì)象(定義時(shí)決定的)

  • 箭頭函數(shù)不能通過bind、call、apply來改變this的值,但依然可以調(diào)用這幾個(gè)方法(只是this的值不受這幾個(gè)方法控制)

“ES6箭頭函數(shù)如何使用”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

標(biāo)題名稱:ES6箭頭函數(shù)如何使用
本文路徑:http://muchs.cn/article32/picjpc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、商城網(wǎng)站、做網(wǎng)站、搜索引擎優(yōu)化、用戶體驗(yàn)關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營