Functions & Closures

`this` binding

Rules that determine what `this` refers to inside a function call.

  1. new Foo()this is the new instance.
  2. obj.method()this is obj.
  3. fn.call(ctx) / fn.apply(ctx) / fn.bind(ctx)this is ctx.
  4. Bare fn()this is undefined in strict mode, the global object otherwise.
  5. Arrow functions — inherit this from the enclosing lexical scope.

Interview questions

Sign in to save

Why can arrow functions not be used as constructors?

They don't have their own `this` or `[[Construct]]` internal method, so `new` throws.

Closures