JavaScriptFundamental (ES6), part 1
Default parameter scope in JavaScript
32 The scope a default lives in
hardA default can read a parameter to its left. Find out what happens when it reads one to its right, when it reads a var from the body, and which duplicate or shadowed parameter names the parser refuses.
Stuck? Three hints, smallest first
- 1. Parameters are initialised left to right, so an earlier one is available and a later one is still in its dead zone.
- 2. When a function has defaults, its parameters live in their own scope, outside the body’s.
- 3. Duplicate parameter names are a sloppy-mode leftover. Strict mode refuses them, and so does any parameter list with a default or a rest element.
const parses = (src) => {
try { new Function(src); return 'parsed'; } catch (e) { return e.constructor.name; }
};
const attempt = (fn) => {
try { return fn(); } catch (e) { return e.constructor.name; }
};
const area = (w, h = w) => w * h; // reads the parameter to its left
const forwardParses = parses('function f(a = b, b = 1) { return [a, b]; }');
const forwardRuns = attempt(() => {
const f = (a = b, b = 1) => [a, b];
return f();
});
const bodyVar = attempt(() => {
function g(a = inner) { var inner = 1; return a; }
return g();
});
const letShadow = parses('function h(a) { let a = 1; }');
const varShadow = parses('function h(a) { var a = 1; return a; }');
const dupSloppy = parses('function f(a, a) { return a; }');
const dupStrict = parses('"use strict"; function f(a, a) { return a; }');
const dupWithDefault = parses('function f(a, a = 1) { return a; }');
console.log('reads to the left :', area(3), area(3, 4));
console.log('reads to the right:', forwardParses, 'then', forwardRuns);
console.log('reads a body var :', bodyVar);
console.log('let over a param :', letShadow, '| var over a param:', varShadow);
console.log('duplicate names :', dupSloppy, 'sloppy,', dupStrict, 'strict,', dupWithDefault, 'with a default');
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('a default may read a parameter to its left', area(3), 9);
test('and is ignored when a value is supplied', area(3, 4), 12);
test('reading one to its right is legal syntax', forwardParses, 'parsed');
test('but throws at call time, from the dead zone', forwardRuns, 'ReferenceError');
test('a default cannot see a var declared in the body', bodyVar, 'ReferenceError');
test('a let in the body may not shadow a parameter', letShadow, 'SyntaxError');
test('a var may, because it is the same binding', varShadow, 'parsed');
test('duplicate parameter names survive only in sloppy mode', [dupSloppy, dupStrict], ['parsed', 'SyntaxError']);
test('and never once any parameter has a default', dupWithDefault, 'SyntaxError');
Once a parameter list contains a default, the parameters get a scope of their own, sitting between the enclosing scope and the function body. That is the whole explanation for two of these results: a = b hits b's temporal dead zone because initialisation is strictly left to right, and a = inner cannot see the body's var inner because the body's variables are declared one scope inward.
Note that new Function bodies are sloppy unless the source says 'use strict', which is what lets the duplicate-parameter case be shown at all — in this page's own panels, every one of which is strict, it would simply refuse to load.