Last updated 2 years ago
JS ⟩ variable ⟩ shadowing ⟩ let can't shadow parameter even in sloppy mode.
let can't shadow parameter(s) even in sloppy mode
function declaration instantiation
lexical environment
parameter / argument
strict mode
stop using var❗️
var can shadow parameter even in strict mode❗️
replit:let can't shadow parameters
'use strict'; // ⭐ toggle sloppy/strict mode const { log } = console; // ⭐ only "simple" parameters // "parameters" / "body declarations" in the same environment record. function f(x) { let x = 1; // ⛔ SyntaxError log(x); } // ⭐ "non-simple" parameters exist, // "parameters" / "body declarations" in different environment records. function g(x = 0) { let x = 100; // ⛔ SyntaxError log(x); } function h(x = 0) { var x = 300; // ❗ "var" do shadow parameter (even in strict mode) log(x); } function k(x) { var x = 400; // ❗ "var" do shadow parameter log(x); } // mode: sloppy strict // ----------------------------------------------- f(10); // ⛔ SyntaxError ⛔ SyntaxError g(20); // ⛔ SyntaxError ⛔ SyntaxError h(30); // ✅ 300 ✅ 300 k(40); // ✅ 400 ✅ 400 // ⛔ SyntaxError: Identifier 'x' has already been declared
JS spec ⟩
FunctionDeclarationInstantiation
Function Environment Record
shadowing parameters by local variables? (my question)
Where are arguments positioned in the lexical environment? ⭐️
YDKJS: Scope & Closures (v.2) ⟩ Ch. 8 ⟩ Parameter Scope