1. this指向
箭頭函數(shù)體內(nèi)的this對象爹袁,就是定義時所在的對象,而不是使用時所在的對象司蔬。
function make () {
return () => {
console.log(this)
}
}
make()() // window
const testFunc = make.call({ name: 'foo' });
testFunc(); // { name: 'foo' }
testFunc.call({ name: 'bar' }); // { name: 'foo' }
testFunc(); // { name: 'foo' }
const testFunc2 = make.call({ name: 'too' });
testFunc2() // { name: 'too' }
如果要綁定this對象
function make () {
var self = this;
return function () {
console.log(self);
}
}
方法二
function make () {
return function () {
console.log(this);
}.bind(this);
}
箭頭函數(shù)不可以使用類似于arguments對象(super(ES6)它掂,new.target(ES6)……),該對象在函數(shù)體內(nèi)不存在晶密。如果要用,可以用Rest參數(shù)代替镊屎。
不可以使用yield命令惹挟,因此箭頭函數(shù)不能用作Generator函數(shù)。
箭頭函數(shù)不可以當作構(gòu)造函數(shù)缝驳,也就是說连锯,不可以使用new命令归苍,否則會拋出一個錯誤。