在查看 Mozilla 文档中的 ES6 箭头函数文档时,我了解到箭头函数应用了严格模式的所有规则,除了链接中描述的规则
var f = () => { 'use strict'; return this};
var g = function () { 'use strict'; return this;}
console.log(f()); //prints Window
console.log(g()); // prints undefined
//we can test this in firefox!
但是,Babel.js
将箭头函数代码转换为返回undefined
而不是Window
(演示链接)的 ES5 代码
"use strict";
setTimeout(function () {
return undefined;
}, 100);
所以,上面的代码片段是 Babel.js 的输出。不能是下面的输出吗?
"use strict";
setTimeout(function () {
return this;
}.bind(Window), 100);
如果我正在编写 ES6,我会期待Window
而不是undefined
它是一个错误吗?
或者,我误解了什么?