The four ways this binds
this is determined by how a function is invoked:
// 1. Default: standalone call
fn(); // undefined (strict) / global
// 2. Method call
obj.method(); // obj
// 3. new
new Fn(); // the new instance
// 4. explicit: bind/call/apply
fn.call(obj); // obj
Arrow functions ignore all of these and always use the lexical this from their definition site. Prefer arrow functions for callbacks to avoid accidental binding loss.
Scope chains
Variables resolve outward through nested scopes:
const global = "G";
function outer() {
const mid = "M";
function inner() {
console.log(global, mid); // both visible
}
inner();
}
Each scope can see variables from its parent scopes. Closures extend this: a returned inner function keeps its parent scope alive. This is the foundation of module patterns and state management.