JavaScript·Intermediate·Coding·1 min read
How is this determined in JavaScript?
Short interview answer
For ordinary functions, this is determined by the call form: new binding, explicit call/apply/bind, method-style implicit binding, or the default rule. Arrow functions do not create their own this; they resolve it lexically from the surrounding scope.
Example
function whoAmI() { return this; }
whoAmI(); // undefined (strict) or global (sloppy) — defaultwhoAmI.call({ id: 1 }); // { id: 1 } — explicitconst o = { whoAmI };o.whoAmI(); // o — implicit (method call)new whoAmI(); // the new instance — new bindingKey takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.