Question presented to candidate: "If I define a plain function and call it with no receiver -- just fn(), not obj.fn() -- what does this refer to inside it? Does use strict change that, and why would that matter for a real bug, like a detached object method?"
What a strong answer should cover:
- In non-strict (sloppy) mode, calling a regular function with no receiver sets this to the global object -- globalThis in Node and modern browsers, window historically in browsers.
- In strict mode, the same bare call leaves this as undefined instead of substituting the global object -- this is called default binding, and strict mode simply skips the substitution step.
- The classic real bug this explains: extracting a method off an object (const fn = obj.method) and calling it bare loses the receiver. In strict mode this throws when the code tries to use this.something (TypeError: Cannot read properties of undefined); in non-strict mode it silently uses the global object instead, which is arguably worse because it fails silently rather than loudly.
- Strict mode also skips auto-boxing a primitive passed as this via call/apply -- a primitive stays a primitive in strict mode, but gets wrapped in its object wrapper (Number, String, Boolean) in non-strict mode.
- ES2015 modules and class bodies are implicitly strict with no pragma needed -- worth knowing this already applies to nearly all modern code without anyone writing use strict by hand.
Clarifying questions expected:
- "Are we talking about a plain function call, or also arrow functions?" -- arrow functions never have their own this at all, so this specific default-binding rule does not apply to them regardless of strict mode.
- "Is the use strict pragma placed once per function, or should I assume the whole file or module is strict?" -- placement matters; a pragma only takes effect if it is the literal first statement of the function or file.
Code / implementation expected: Yes -- a real, executed comparison of a bare call in a strict function versus a non-strict function, plus a detached-method example showing the real consequence of each mode.