this, Prototypes and Classes
this and prototypes are the two parts of JavaScript that most resemble a different language hidden
inside it. Both are simpler than their reputation once you separate the rules from the syntax: this
is decided at call time by four rules, and inheritance is a chain of objects.
The 60-second answer
Section titled “The 60-second answer”“this is determined by how a function is called, not where it is defined, with one exception:
arrow functions take this lexically. In priority order: new binding, explicit binding via
call/apply/bind, implicit binding through obj.method(), then the default — undefined in
strict mode and modules, globalThis in sloppy mode. Inheritance is the prototype chain: property
lookup walks from the object to its prototype until it finds the key. class is syntax over that
mechanism; methods live on the prototype, and extends wires up the chain and super.”
The four binding rules
Section titled “The four binding rules”| Rule | Call form | this |
|---|---|---|
new |
new Fn() |
The newly created object |
| Explicit | fn.call(obj), fn.apply(obj, args), fn.bind(obj) |
The object you passed |
| Implicit | obj.method() |
obj (the receiver before the dot) |
| Default | fn() |
undefined (strict / modules) or globalThis (sloppy) |
Priority is top to bottom: new beats bind, and bind beats an implicit receiver.
'use strict';const user = { name: 'Ada', greet() { return `Hi, ${this.name}`; } };
user.greet(); // 'Hi, Ada' — implicitconst detached = user.greet;detached(); // TypeError: this is undefinedconst bound = user.greet.bind(user);bound(); // 'Hi, Ada' — explicitnew bound(); // `new` still wins for the constructed objectThe detached-callback bug is the most common this question, and it appears in real code every time
you pass an object method to setTimeout, addEventListener or map.
call, apply, bind
Section titled “call, apply, bind”call takes arguments listed, apply takes them as an array, and bind returns a new function with
the receiver fixed — which is why it cannot be re-bound, and why class constructors used to contain
this.handleClick = this.handleClick.bind(this) before class fields (handleClick = () => {})
replaced the pattern. The three-line version is fn.call(obj, a, b), fn.apply(obj, [a, b]) and
const g = fn.bind(obj), and an interview answer rarely needs more than that.
Arrow functions
Section titled “Arrow functions”Arrow functions differ in four ways: no own this (captured lexically), no arguments object,
cannot be used with new, and no own prototype property.
const timer = { seconds: 0, start() { setInterval(() => { this.seconds++; }, 1000); // lexical `this` = timer },};Use arrow functions for callbacks that should keep the surrounding this; use method shorthand for
object methods that should receive their caller.
The prototype chain
Section titled “The prototype chain”Every object has an internal [[Prototype]] link, readable with Object.getPrototypeOf(obj) (and
via the legacy __proto__ accessor). Property access walks that chain.
const animal = { speak() { return `${this.name} makes a sound`; } };const dog = Object.create(animal);dog.name = 'Rex';
dog.speak(); // inheritedObject.getPrototypeOf(dog) === animal; // trueObject.hasOwn(dog, 'speak'); // false'speak' in dog; // true — walks the chainConfusing pair: Fn.prototype is the object instances link to; Object.getPrototypeOf(fn) is the
prototype of the function itself (usually Function.prototype). Assigning dog.speak = ...
creates an own property and shadows the inherited one.
new Fn() does four things: create an object linked to Fn.prototype, bind this to it, run the
body, and return that object unless the body returns a different object.
class syntax
Section titled “class syntax”class is not a new object model. It is stricter, clearer sugar.
abstract class Shape { #id = crypto.randomUUID(); // truly private field constructor(public readonly name: string) {} abstract area(): number; describe() { return `${this.name} (${this.#id}): ${this.area().toFixed(2)}`; }}
class Circle extends Shape { constructor(private radius: number) { super('circle'); } area() { return Math.PI * this.radius ** 2; }}What class changes versus constructor functions: the body is strict mode; methods are
non-enumerable; declarations sit in the temporal dead zone, so they cannot be used before evaluation;
calling without new throws; and extends links both the instance chain and the static side, so
static methods are inherited too.
If a subclass defines a constructor it must call super() before touching this. Class fields are
initialised after super() returns, which is why field initialisers cannot read this before that.
Follow-up questions
Section titled “Follow-up questions”- What does
newdo, step by step? - Why does
bindnot work twice, and why doesnewstill override it? - What is the difference between
Fn.prototypeandObject.getPrototypeOf(Fn)? - How does
extendswire up static inheritance? - Why can an arrow function not be a constructor?
Common traps
Section titled “Common traps”- “
thisis the function’s own scope.”thisis a call-site parameter, not a scope. - “Arrow functions have their own
thisbut ignore it.” They have no ownthisat all. - “
classintroduces a new inheritance model.” It compiles to the prototype chain. - “Private
#fieldis just a naming convention.” It is engine-enforced;_fieldis the convention. - Forgetting
super(). Subclass constructors throw if they usethisbeforesuper().