1. 程式人生 > >箭頭函式和普通函式的區別,this的指向問題

箭頭函式和普通函式的區別,this的指向問題

1.箭頭函式作為匿名函式,不能作為建構函式,不能使用new

var B = ()=>{
  value:1;
}

var b = new B(); //TypeError: B is not a constructor

2.箭頭函式不繫結arguments,取而代之用rest引數…解決

function A(a){
  console.log(arguments); //[object Arguments] {0: 1}
}

var B = (b)=>{
  console.log(arguments); //ReferenceError: arguments is not defined
}

var C = (...c)=>{ //...c即為rest引數
  console.log(c); //[3]
}
A(1);
B(2);
C(3);

3.箭頭函式會捕獲上下文this作為自己的this,this繼承自外圍作用域。在箭頭函式中呼叫 this 時,僅僅是簡單的沿著作用域鏈向上尋找,找到最近的一個 this 拿來使用。 4.箭頭函式沒有原型屬性。 5.箭頭函式無法換行。