The version of Node.js you're using (0.12.9) uses an older version of the V8 JavaScript engine (3.28.71.19) which does not follow the new function declaration scoping rules added in ECMAScript 6. The version of Chrome you're using (49) uses a new version of V8 (4.9.385) which does support the new rules, at least in strict mode.
Prior to ECMAScript 6, function declarations would be scoped to the containing function. For example:
ECMAScript 5
function main() {
if (true) {
function example() {};
console.log(example); // function example() {}
}
console.log(example); // function example() {}
}
This was considered confusing behaviour, and so it was prohibited in strict mode, leading to the error you see in Node.
In ECMAScript 6, function declarations are instead scoped to the nearest block. A block is any series of statements contain between two brackets ({ ... }
), such as following an if
statement.
ECMAScript 6
function main() {
'use strict';
if (true) {
function example() {};
console.log(example); // function example() {}
}
console.log(example); // ReferenceError: example is not defined
}
This behaviour is more intentional and less confusing, so it is permitted in strict mode.
However, testing this is a little bit confusing, because Chrome currently enables some ES6 rules only in strict mode.