While looking into ES6 arrow functions' documentation on Mozilla documentation, I got to know that Arrow functions applies all the rules of strict mode, except one as described in the link
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!
But, Babel.js
is transpiling the arrow function code to ES5 code that returns undefined
rather than Window
(demo link)
"use strict";
setTimeout(function () {
return undefined;
}, 100);
So, the above snippet is the output from Babel.js. Couldn't it be the below output?
"use strict";
setTimeout(function () {
return this;
}.bind(Window), 100);
If I am writing ES6, I would expect Window
rather than undefined
Is it a bug?
OR, I misunderstood anything?
tl;dr: Babel assumes every file is a module. Modules are strict by default and their
this
value isundefined
.This is covered in the Babel FAQ: