Values, references and scope
This part of the JavaScript course is about what a name holds. An object handed to a function is shared rather than copied, the spread operator and Object.assign copy one level, structuredClone copies all of them, and const protects the binding rather than the object, so Object.freeze comes next. Block scope and shadowing explain where a name ends, a closure keeps a variable alive after the call that made it, this is set by the call, and strict mode removes the old defaults. Destructuring and the three dots of spread and rest close the part.
2.1
References and sharing: one object, two names
a binding holds a reference, primitives against objects, === as identity, a function that changes the object of its caller
Open
2.2
Copying values: spread and Object.assign
what copies and what shares, the spread copy of an array and an object, Object.assign onto an empty target, a shallow copy over a shared nested object
Open
2.3
Deep copies with structuredClone
why one level is not enough, structuredClone over nested objects and dates, DataCloneError on a function, a class instance without its prototype, the JSON round trip and its losses
Open
2.4
const is not frozen
the TypeError of a second assignment to the name, object and array contents that still change, a const object that grows as a counter
Open
2.5
Freezing objects with Object.freeze
read-only properties, a refused write ignored in sloppy mode and thrown in strict, the shallow freeze, a deep freeze written by hand
Open
2.6
Block scope and shadowing
let and const per block, the name that ends at the brace, shadowing in a function and a block, the loop binding that let makes per pass
Open
2.7
Closures: a function keeps its variables
a function born with its scope, variables that outlive the call, one environment per call, two functions over one variable, what a closure keeps alive
Open
2.8
Closures in practice: counters and factories
a counter with private state, the module pattern behind an IIFE, a function factory, a once wrapper that remembers its result
Open
2.9
this: how the call sets it
the method call, the plain call that gets undefined, the arrow that keeps the outer this, call, apply and bind
Open
2.10
Strict mode
sloppy mode as the old default, the directive in a file and in a function, a module as strict code, this and delete and the undeclared name
Open
2.11
Destructuring: arrays, objects and defaults
array patterns with a hole and a rest element, a swap, object patterns with renaming and defaults, a nested pattern, destructured parameters
Open
2.12
Spread and rest: three dots in both directions
spread in a call, in an array and in an object, the order that decides an override, rest parameters and rest elements in patterns
Open