ES6 箭头函数:
'use strict';
var obj = {
i: 10,
b: () => console.log(this.i, this),
c: function() {
console.log( this.i, this)
}
}
obj.b(); // 为什么 输出undefined, Window对象;
而obj.c()
则输出 : 10, obj
;
我之前把上面的代码理解为:
'use strict';
var obj = {
i: 10,
b: function() {
return console.log( this.i, this)
},
c: function() {
console.log( this.i, this)
}
}
但是我发现, 执行obj.b();
后输出10, obj
;
箭头函数使用注意点:
(1)函数体内的this对象,就是定义时所在的对象,而不是使用时所在的对象。
(2)不可以当作构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。
(3)不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。
(4)不可以使用yield命令,因此箭头函数不能用作 Generator 函数。
上面四点中,第一点尤其值得注意。this对象的指向是可变的,但是在箭头函数中,它是固定的。