Why do ES6 code and ES5 code have excellent result after compiling with Babel.js?

ES6 Code:

let foo = 'outer'; function bar(func = x => foo){ let foo = 'inner'; console.log(func()); } bar(); // outer 

The result is "external."

ES5 code compiled by Babel.js:

 'use strict'; var foo = 'outer'; function bar() { var func = arguments.length <= 0 || arguments[0] === undefined ? function (x) { return foo; } : arguments[0]; var foo = 'inner'; console.log(func()); } bar(); // inner 

The result is "external."

I do not know why they have a different result.

+6
source share
1 answer

This is a mistake in Babel . Expressions in lists of complex parameters should not see declarations in the function body, but the code created by Babel here evaluates the default parameter in the scope of the function, where the internal foo displayed.

+5
source

All Articles