Skip to content

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.

“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.”

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.

this-rules.js
'use strict';
const user = { name: 'Ada', greet() { return `Hi, ${this.name}`; } };
user.greet(); // 'Hi, Ada' — implicit
const detached = user.greet;
detached(); // TypeError: this is undefined
const bound = user.greet.bind(user);
bound(); // 'Hi, Ada' — explicit
new bound(); // `new` still wins for the constructed object

The 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 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 differ in four ways: no own this (captured lexically), no arguments object, cannot be used with new, and no own prototype property.

arrow-this.js
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.

Every object has an internal [[Prototype]] link, readable with Object.getPrototypeOf(obj) (and via the legacy __proto__ accessor). Property access walks that chain.

prototype-chain.js
const animal = { speak() { return `${this.name} makes a sound`; } };
const dog = Object.create(animal);
dog.name = 'Rex';
dog.speak(); // inherited
Object.getPrototypeOf(dog) === animal; // true
Object.hasOwn(dog, 'speak'); // false
'speak' in dog; // true — walks the chain

Confusing 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 is not a new object model. It is stricter, clearer sugar.

shapes.ts
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.

  1. What does new do, step by step?
  2. Why does bind not work twice, and why does new still override it?
  3. What is the difference between Fn.prototype and Object.getPrototypeOf(Fn)?
  4. How does extends wire up static inheritance?
  5. Why can an arrow function not be a constructor?
  • “this is the function’s own scope.” this is a call-site parameter, not a scope.
  • “Arrow functions have their own this but ignore it.” They have no own this at all.
  • “class introduces a new inheritance model.” It compiles to the prototype chain.
  • “Private #field is just a naming convention.” It is engine-enforced; _field is the convention.
  • Forgetting super(). Subclass constructors throw if they use this before super().