JS: prototype (property)

By Xah Lee. Date: . Last updated: .

Where does property key named "prototype" came from

Every function defined by keyword function has a property key "prototype", by spec, automatically created by JavaScript.

// every function defined by keyword function has a property key "prototype"
console.assert((Object.hasOwn(function () {}, "prototype")) === true);

// arrow function does not
console.assert(Object.hasOwn((x) => (x + 1), "prototype") === false);

also, standard object's constructors all have property key "prototype".

// standard object's constructors all have property key "prototype"
[Function, Array, Object, Date].forEach((x) => {
 console.assert((Object.hasOwn(x, "prototype")) === true);
});

Purpose

The property key "prototype" is a mechanism for object created using Constructor to link to a parent.

In other words, the property string key "prototype", is used to check which object should be the parent object, when a object is created with the with operator new.

// create a constructor
function Xcons() {}

// js auto creates a property string key "prototype"
const xdad = Xcons["prototype"];
console.assert(xdad);

// create a object
const jj = new Xcons();

// check the parent object is Xcons.prototype
console.assert(Reflect.getPrototypeOf(jj) === xdad);

You can change the prototype before calling constructor, so that the newly created object's parent is that value.

F.prototype = obj

Default Value of Property Key "prototype"

For user-defined function f, the value of f.prototype by default is a newly created data object {"constructor":f}.

// Default Value of Property Key "prototype"

// every constructor has a property key "prototype"
function Xcons() {}
console.assert((Object.hasOwn(Xcons, "prototype")) === true);

// value of Xcons.prototype
const xdad = Xcons.prototype;

// its value is a data object
console.assert(Reflect.apply(Object.prototype.toString, xdad, []) === "[object Object]");

// with just 1 property
console.assert(Object.getOwnPropertyNames(xdad).length === 1);

// the property key is string "constructor"
console.assert((Object.hasOwn(xdad, "constructor")) === true);

// the value of the property key "constructor" is the function
console.assert(xdad.constructor === Xcons);

// show its attributes
{
 const xdesc = Reflect.getOwnPropertyDescriptor(xdad, "constructor");
 console.assert(xdesc.writable === true);
 console.assert(xdesc.enumerable === false);
 console.assert(xdesc.configurable === true);
}

// show its parent
console.assert(Reflect.getPrototypeOf(xdad) === Object.prototype);

Property Key "prototype" of Standard Object's Constructors

For any Standard Object's Constructor B, the value of B.prototype is a object, and the only way to express that object is just via the “prototype” property access, that is B.prototype.

For example:

And, just like user-defined function, the following is true:

For any JavaScript's buildin constructor B, the parent of new B() is always B.prototype. Because the value B.prototype can never change.

// For any JavaScript's buildin constructor B, the parent of new B(…) is always B.prototype.
// Because the value B.prototype can never change.

[Object, Function, Array, Date, RegExp]
 .forEach((x) => {
  console.assert(
   Reflect.getPrototypeOf(Reflect.construct(x, [])) === x.prototype,
  );
 });

Standard Constructor's Prototype Property Cannot be Changed

Standard constructor's prototype property cannot be changed, because its writable attribute is false.

console.assert(Reflect.getOwnPropertyDescriptor(Array, "prototype").writable === false);

〔see Property Attributes

JavaScript. Constructor, Class