Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Function.prototype and class constructor syntax

Distinguish a function's own prototype chain from the `.prototype` property used to create instances, and see how `new`, `constructor`, and `super()` fit into inheritance.

Version: ECMAScript 2024 and the current web platform; runtime support varies

Syntax

function C(value) {
  this.value = value;
}
C.prototype.read = function () {
  return this.value;
};
const instance = new C(1);

Function.prototype
Object.getPrototypeOf(C)
C.prototype
Object.getPrototypeOf(instance)

class Base {
  constructor(value) {
    this.value = value;
  }
  read() {
    return this.value;
  }
}
class Derived extends Base {
  constructor(value) {
    super(value);
    this.ready = true;
  }
}
const derived = new Derived(2);

Function.prototype is the built-in function object from which functions inherit methods such as call, apply, and bind. It is the direct prototype of a plain function declaration and of a class with no extends clause. Calling Function.prototype() returns undefined, but new Function.prototype() throws TypeError.

C.prototype is different: it is an own property of an ordinary constructor function or class C. If its value is an object, new C(...) normally uses it as the instance's prototype. Inspect that link with Object.getPrototypeOf(instance). A constructor can explicitly return a different object instead. Adding .prototype to an arrow function doesn't make it usable with new.

Inside a class, constructor(...) { ... } initializes the instance, typically by assigning to this. Public instance methods like read() go on C.prototype; static methods go on C. Calling a class directly as C(...) throws TypeError, unlike calling an ordinary function. You can replace an ordinary function's .prototype without changing the prototype of existing instances. A class's .prototype property is not writable, but the prototype object itself can still be modified.

In class Derived extends Base, Derived inherits from Base, while Derived.prototype inherits from Base.prototype. In an explicit derived constructor, call super(...) before using this; using this earlier throws ReferenceError. The exception is when you return an object directly without using this: you can skip super(). If you omit the derived constructor altogether, the default constructor forwards the arguments.

Examples

{
  function Legacy(name) {
    this.name = name;
  }
  Legacy.prototype.read = function () {
    return this.name;
  };

  class Modern {
    constructor(name) {
      this.name = name;
    }
    read() {
      return this.name;
    }
  }

  const a = new Legacy('Ada');
  const b = new Modern('Lin');
  const prototypeOf = Object.getPrototypeOf;
  const descriptor = Object.getOwnPropertyDescriptor;
  console.log(`${prototypeOf(Legacy) === Function.prototype} ${prototypeOf(Modern) === Function.prototype}`);
  console.log(`${prototypeOf(a) === Legacy.prototype} ${prototypeOf(b) === Modern.prototype}`);
  console.log(`${a.read()} ${b.read()}`);
  console.log(`${descriptor(Legacy, 'prototype').writable} ${descriptor(Modern, 'prototype').writable}`);
  try {
    Modern('Lin');
  } catch (error) {
    console.log(error.name);
  }
}

Result

true true
true true
Ada Lin
true false
TypeError

Run the whole block in a console that supports `class` syntax. The first two lines compare constructor and instance prototypes; the last line shows the error name without relying on its message.

Related lessons

  • JavaScript · Lesson 32 · Advanced · Version: 4

Sources