this-bindingmedium
How is the value of `this` determined?
Answer
For a normal function it's decided by how it's called, not where it's written: called as obj.method() it's obj; called bare it's undefined in strict mode; with new it's the new instance; and call/apply/bind set it explicitly. Arrow functions have no own this — they use the enclosing scope's, which is exactly why they're the safe choice for callbacks inside a method.
Points an interviewer listens for
- Determined at call time for normal functions
- Method call → the object; bare call → undefined in strict mode
- new → the instance; call/apply/bind → explicit
- Arrows inherit this from the enclosing scope
Reported at
Similar questions have been reported by candidates at HGS.
Now try answering it out loud
Interview practice mode times your spoken answer and checks it against the points above.
Practise this question