欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

javascript中的箭頭函數(shù)基礎語法及使用場景示例

 更新時間:2023年07月18日 15:14:04   作者:風如也  
這篇文章主要為大家介紹了?javascript中的箭頭函數(shù)基礎語法及使用場景示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

箭頭函數(shù)

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

語法

(param1, param2, …, paramN) => { statements }
(param1, param2, …, paramN) => expression
//相當于:(param1, param2, …, paramN) =>{ return expression; }
// 當只有一個參數(shù)時,圓括號是可選的:
(singleParam) => { statements }
singleParam => { statements }
// 沒有參數(shù)的函數(shù)應該寫成一對圓括號。
() => { statements }
//加括號的函數(shù)體返回對象字面量表達式:
params => ({foo: bar})
//支持剩余參數(shù)和默認參數(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

箭頭函數(shù)的作用:更簡短的函數(shù)且不綁定 this。

對于普通函數(shù)來說,內(nèi)部的 this 指向函數(shù)運行時所在的對象,但是箭頭函數(shù)不是。

箭頭函數(shù)不會創(chuàng)建自己的 this,它只會從自己的作用域鏈的上一層繼承 this。

箭頭函數(shù)內(nèi)部的 this 指向是固定的,普通函數(shù)的 this 指向是可變的。

function foo() {
  setTimeout(() => {
    console.log('id:', this.id);
  }, 100);
}
var id = 21;
foo.call({ id: 42 }); // id: 42

上面代碼中,setTimeout() 的參數(shù)是一個箭頭函數(shù),這個箭頭函數(shù)的定義生效是在 foo 函數(shù)生成時,而它的真正執(zhí)行要等到 100 毫秒后。如果是普通函數(shù),執(zhí)行時 this 應該指向全局對象 window,這時應該輸出 21。但是,箭頭函數(shù)導致 this 總是指向函數(shù)定義生效時所在的對象(本例是 {id: 42}),所以打印出來的是 42。

通過 call 或 apply 或 bind 調(diào)用

由于箭頭函數(shù)沒有自己的 this 指針,通過 call() 或 apply() 方法調(diào)用一個箭頭函數(shù)時,只能傳遞參數(shù)(不能綁定 this),他們的第一個參數(shù)會被忽略(這種現(xiàn)象對于 bind 方法同樣成立)。

var adder = {
  base : 1,
  add: function(a) {
    var f = v => v + this.base;
    return f(a);
  },
  addAnotherThis: function(a) {
    var f = v => v + this.base;
    var b = {
      base : 2
    };
    // return f.call(b, a);
    // return f.apply(b, [a]);
    return f.bind(b, a)();
  }
};
console.log(adder.add(1));         // 輸出 2
console.log(adder.addAnotherThis(1)); // 輸出 2

不綁定 arguments

箭頭函數(shù)不綁定 arguments 對象。此外,super,new.target 在箭頭函數(shù)中也不存在。

var arguments = [1, 2, 3];
var fn = () => arguments[0];
// arguments 只是引用了封閉作用域內(nèi)的 arguments
fn(); // 1
function getArgs(n) {
  // 隱式綁定 getArgs 函數(shù)的 arguments 對象。arguments[0] 是 n
  // 即傳給 getArgs 函數(shù)的第一個參數(shù)
  var f = () => arguments[0] + n;
  return f();
}
getArgs(1); // 2
getArgs(2); // 4
getArgs(3); // 6
getArgs(3,2);//6

在箭頭函數(shù)中,相比于使用 arguments 對象來說,使用剩余參數(shù)是更好的選擇。

function getArgs(n) {
  var f = (...args) => args[0];
  return f();
}
getArgs(1); // 1
getArgs(2); // 2
const numbers = (...nums) => nums;

使用箭頭函數(shù)作為方法

箭頭函數(shù)表達式對非方法函數(shù)是最合適的。

示例1:

var obj = {
  a: 10,
  b: () => console.log(this.a, this),
  c: function() {
    console.log(this.a, this)
  }
}
obj.b();
// undefined, Window{...}
obj.c();
// 10, Object {...}

示例2:

let obj = {
  a: 10
};
Object.defineProperty(obj, "b", {
  get: () => {
    console.log(this.a, typeof this.a, this);
    return this.a + 10;
    // this 代表全局對象 'Window', 因此 'this.a' 返回 'undefined'
  }
});

不能使用 new 操作符

箭頭函數(shù)不能用作構(gòu)造器,即不能和 new 一起使用。

let Apple = () => {}
let apple = new Apple() // Uncaught TypeError: Apple is not a constructor

不能使用 prototype 屬性

箭頭函數(shù)沒有 prototype 屬性,因為不能用作構(gòu)造函數(shù)。

let far = function() {}
console.log(far.prototype) // {constructor: ?}
let Apple = () => {}
console.log(Apple.prototype) // undefined

不能用作函數(shù)生成器

函數(shù)生成器用作通常不能在箭頭函數(shù)中使用,除非是嵌套在允許使用的函數(shù)內(nèi)。因此,箭頭函數(shù)不能用作函數(shù)生成器。

返回對象字面量

let apple = () => ({ height: 100 })

箭頭函數(shù)返回對象字面量,必須用圓括號把對象字面量包起來。

params => { objectKey: objectValue } 這種語法返回對象是錯誤的。因為花括號{}里面的代碼被解析為一系列語句(代碼塊),所以如果箭頭函數(shù)直接返回一個對象,必須在對象外面加上括號。

換行

箭頭函數(shù)中參數(shù)和箭頭之間不能換行。

錯誤方式:

let apple = () 
=> ({ height: 100 }) // Uncaught SyntaxError: Unexpected token '=>'

可以在 => 之后換行,或者使用 () 或 {} 換行。

正確方式:

let apple = () => 
({ height: 100 })
let apple = (
  s, a, b
) => 
({ height: 100 })
let apple = () => {
  // ...
}

箭頭函數(shù)不適用場合

定義對象的方法,且方法內(nèi)部包含 this,不適用箭頭函數(shù)

const obj = {
  a: 1,
  fn: () => {
    this.a++
  }
}
console.log(obj.fn()) // undefined

因為對象不構(gòu)成單獨的作用域,導致 fn 箭頭函數(shù)中定義時的作用域就是全局作用域,fn

里面的 this 指向全局對象。如果 fn 是普通函數(shù),方法內(nèi)部的 this 執(zhí)行調(diào)用時的作用域,即obj。

需要動態(tài) this 的時候,不適用箭頭函數(shù)

var button = document.getElementById('press');
button.addEventListener('click', () => {
  this.classList.toggle('on');
});

代碼運行時,點擊按鈕會出現(xiàn)報錯,因為 button 的監(jiān)聽函數(shù)是一個箭頭函數(shù),它的 this 指向全局對象。如果改成普通函數(shù),this 就會動態(tài)指向被點擊的按鈕對象。

相關文章

最新評論