To expand on Bergis answer, there is a difference in how the code is interpreted in ES5 and ES6 since block-scoped function declarations were added.
Input:
function test() {
try {
function _free() { }
var _free = 1;
} finally { }
}
Since ES5 does not support block-level functions, _free
is hoisted to the parent function:
function test() {
var _free = function _free() { }
try {
var _free = 1;
} finally { }
}
In ES6, the function is declared at block-level, and semantically equal to a let
/const
declaration:
function test() {
try {
let _free = function _free() { }
var _free = 1;
} finally { }
}
This throws an error because var _free
tries to declare a variable which is already declared.
For example, this throws in ES6 as well:
let _free;
var _free = 1; // SyntaxError: Indentifier '_free' has already been declared
While this is fine:
var _free;
var _free = 1; // No SyntaxError
Setting the value of the already declared identifier is fine:
let _free;
_free = 1;
Therefore, to set the declared identifier _free
to 1, you need to skip the second declaration:
try {
function _free() { }
_free = 1;
} finally { }