Function: prototype
Theprototype
data property of aFunction
instance is used when the function is used as a constructor with thenew
operator. It will become the new object's prototype.
Note:Not allFunction
objects have theprototype
property — seedescription.
Value
An object.
Property attributes ofFunction: prototype | |
---|---|
Writable | yes |
Enumerable | no |
Configurable | no |
Note:Classes are a type of function, so most of the description here applies to theprototype
property of classes too. The only salient difference is that theprototype
property of a class is not writable.
Description
When a function is called withnew
, the constructor'sprototype
property will become the resulting object's prototype.
function Ctor() {}const inst = new Ctor();console.log(Object.getPrototypeOf(inst) === Ctor.prototype); // true
You can readInheritance and the prototype chain for more information about the interactions between a constructor function'sprototype
property and the resulting object's prototype.
A function having aprototype
property is not sufficient for it to be eligible as a constructor.Generator functions have aprototype
property, but cannot be called withnew
:
async function* asyncGeneratorFunction() {}function* generatorFunction() {}
Instead, generator functions'prototype
property is used when they are calledwithoutnew
. Theprototype
property will become the returnedGenerator
object's prototype.
In addition, some functions may have aprototype
but throw unconditionally when called withnew
. For example, theSymbol()
andBigInt()
functions throw when called withnew
, becauseSymbol.prototype
andBigInt.prototype
are only intended to provide methods for the primitive values, but the wrapper objects should not be directly constructed.
The following functions do not haveprototype
, and are therefore ineligible as constructors, even if aprototype
property is later manually assigned:
const method = { foo() {} }.foo;const arrowFunction = () => {};async function asyncFunction() {}
The following are valid constructors that haveprototype
:
class Class {}function fn() {}
Abound function does not have aprototype
property, but may be constructable. When it's constructed, the target function is constructed instead, and if the target function is constructable, it would return a normal instance.
const boundFunction = function () {}.bind(null);
A function'sprototype
property, by default, is a plain object with one property:constructor
, which is a reference to the function itself. Theconstructor
property is writable, non-enumerable, and configurable.
If theprototype
of a function is reassigned with something other than anObject
, when the function is called withnew
, the returned object's prototype would beObject.prototype
instead. (In other words,new
ignores theprototype
property and constructs a plain object.)
function Ctor() {}Ctor.prototype = 3;console.log(Object.getPrototypeOf(new Ctor()) === Object.prototype); // true
Examples
Changing the prototype of all instances by mutating the prototype property
function Ctor() {}const p1 = new Ctor();const p2 = new Ctor();Ctor.prototype.prop = 1;console.log(p1.prop); // 1console.log(p2.prop); // 1
Adding a non-method property to a class's prototype property
Class fields add properties to each instance. Class methods declarefunction properties on the prototype. However, there's no way to add a non-function property to the prototype. In case you want to share static data between all instances (for example,Error.prototype.name
is the same between all error instances), you can manually assign it on theprototype
of a class.
class Dog { constructor(name) { this.name = name; }}Dog.prototype.species = "dog";console.log(new Dog("Jack").species); // "dog"
This can be made more ergonomic usingstatic initialization blocks, which are called when the class is initialized.
class Dog { static { Dog.prototype.species = "dog"; } constructor(name) { this.name = name; }}console.log(new Dog("Jack").species); // "dog"
Specifications
Specification |
---|
ECMAScript® 2026 Language Specification # sec-function-instances-prototype |