JavaScript: Property Key "prototype"

By Xah Lee. Date: . Last updated: .

Any constructor function has a magical property key "prototype".

// every function has a property key "prototype" except arrow functions

// standard function objects
console.log( 
 Function.hasOwnProperty ( "prototype" ) &&
 Array.hasOwnProperty ( "prototype" ) &&
 Object.hasOwnProperty ( "prototype" ) &&
 Date.hasOwnProperty ( "prototype" )
);

// user-defined function
console.log( (function () {}).hasOwnProperty("prototype") );

console.log( ! (x => (x+1)).hasOwnProperty("prototype") );

// all true

[see Property Overview]

Here's code to print all global function objects and report if they have key “prototype”.

// if in nodejs, replace all “window” by “global”
// if in browser, replace all “global” by “window”

const listHas = [];
const listNo = [];

( Reflect.ownKeys ( global ) ).forEach (
    (x) => {
        if ( typeof global[x] === "function" ) {
            if ( global[x].hasOwnProperty ( "prototype" ) ) {
                listHas.push(x);
            } else { listNo.push(x); }
        }
    }
)

console.log( "Has key prototype", listHas );

/* [
Has key prototype [
  'Object',         'Function',       'Array',
  'Number',         'Boolean',        'String',
  'Symbol',         'Date',           'Promise',
  'RegExp',         'Error',          'EvalError',
  'RangeError',     'ReferenceError', 'SyntaxError',
  'TypeError',      'URIError',       'ArrayBuffer',
  'Uint8Array',     'Int8Array',      'Uint16Array',
  'Int16Array',     'Uint32Array',    'Int32Array',
  'Float32Array',   'Float64Array',   'Uint8ClampedArray',
  'BigUint64Array', 'BigInt64Array',  'DataView',
  'Map',            'BigInt',         'Set',
  'WeakMap',        'WeakSet',        'SharedArrayBuffer',
  'Buffer',         'URL',            'URLSearchParams',
  'TextEncoder',    'TextDecoder',    'clearInterval',
  'clearTimeout',   'setInterval',    'setTimeout',
  'queueMicrotask', 'clearImmediate', 'setImmediate'
]
] */

console.log( "No key prototype", listNo );

/* [
No key prototype [
  'parseFloat',
  'parseInt',
  'Proxy',
  'decodeURI',
  'decodeURIComponent',
  'encodeURI',
  'encodeURIComponent',
  'escape',
  'unescape',
  'eval',
  'isFinite',
  'isNaN'
]

] */

console.log( process.version )
// v13.6.0

The value of a property key "prototype" of a object X is NOT the prototype (aka parent) of object X. (it's possible to make it so, but that basically never happens.)

[see Prototype and Inheritance]

Where Does Key "prototype" Came From?

Every function has a property key "prototype", by spec.

Purpose

The property key "prototype" is a mechanism for object created using new F() to link to a parent.

You can set a function F's prototype property F.prototype = obj before calling new F(), so that the newly created object's parent will be obj.

[see Operator “new”]

function FF () {};
const x = new FF();
console.log( Reflect.getPrototypeOf(x) === FF.prototype ); // true

[see Create Object with Parent X]

Default Value

For user-defined function f, the value of f.prototype by default is a newly created object {"constructor":f} (that is, a newly created object, with only one property key, this property key is named "constructor", and value of property key "constructor" is the function itself. The parent of this newly created object, is Object.prototype).

// every function has a property key "prototype"
function FF() {};
 console.log( FF.hasOwnProperty("prototype")
); // true

// its value is a object, with just 1 property
console.log( 
 Object.getOwnPropertyNames(FF.prototype).length === 1
); // true

// the property key is "constructor"
console.log( 
 FF.prototype.hasOwnProperty ( "constructor" )
); // true

// the value of the property key "constructor" is the function
console.log( FF.prototype.constructor === FF ); // true

// show its attributes
console.log( 
 Object.getOwnPropertyDescriptor( FF.prototype, "constructor" )
);
// { value: [Function: FF],
//   writable: true,
//   enumerable: false,
//   configurable: true }

// show its parent
console.log( Reflect.getPrototypeOf ( FF.prototype ) === Object.prototype ); // true

[see Property Key “constructor”]

Builtin Function's Value of Property Key "prototype"

For any JavaScript's buildin 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.

console.log( 
Reflect.getPrototypeOf ( new Object() ) === Object.prototype &&
Reflect.getPrototypeOf ( new Function() ) === Function.prototype &&
Reflect.getPrototypeOf ( new Array() ) === Array.prototype &&
Reflect.getPrototypeOf ( new Date() ) === Date.prototype &&
Reflect.getPrototypeOf ( new RegExp() ) === RegExp.prototype 
); // true

Standard Constructor's Prototype Property Cannot be Changed

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

console.log(
Object.getOwnPropertyDescriptor(Array, "prototype").writable === false
); // true

[see Property Attributes]

JavaScript Constructor/Class

JS in Depth
XAH  BUY NOW

JS in Depth

JS Obj Ref

DOM


JS in Depth

Basic Syntax

Value Types

Variable

String

Function

Property

Object and Inheritance

Array

Constructor/Class

Iterable 🌟

Misc