Scope of Default function parameters in javascript
I don't understand why these examples are not good
Because the default initialisers are not evaluated in the parent scope, but rather inside the function scope. The parameters themselves are already in scope, so that you can do something like
(function(a = 2, b = a) { console.log(b); }());
Could somebody please point me to place in specification which could clarify this?
The relevant section is §9.2.12 FunctionDeclarationInstantiation.
I must say that specification is rather hard to understand.
Yes it is, although it's written for engine implementors not for programmers. However, the explanatory note basically confirms your understanding of the optimisation
If the function’s formal parameters do not include any default value initializers then the body declarations are instantiated in the same Environment Record as the parameters. If default value parameter initializers exist, a second Environment Record is created for the body declarations.
Your examples basically desugar to
(function() {
let a = arguments[0] !== undefined ? arguments[0] : b,
// ^ clearly a ReferenceError
b = arguments[1] !== undefined ? arguments[1] : 1;
{
}
})();
(function() {
let c = 1;
return (function() {
let a = arguments[0] !== undefined ? arguments[0] : c;
// ^ works as you'd think
{
return a === 1;
}
})();
})();
(function() {
let a = 1;
(function() {
let a = arguments[0] !== undefined ? arguments[0] : a;
// ^ again clearly a ReferenceError
{
}
})();
})();
(function() {
let b = 1;
(function() {
let a = arguments[0] !== undefined ? arguments[0] : b,
// ^ still a ReferenceError
b = arguments[1] !== undefined ? arguments[1] : 2;
{
}
})();
})();
(function() {
let a = arguments[0] !== undefined ? arguments[0] : () => b,
// ^ works indeed
b = arguments[1] !== undefined ? arguments[1] : 2;
{
return a() === 2;
}
})();