Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Function.prototype.prototype — why it reads as undefined

In an unmodified environment, reading `Function.prototype.prototype` gives `undefined`. That does not mean `Function.prototype` has no internal prototype.

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

Syntax

Function.prototype.prototype

Function.prototype.prototype reads the property named prototype on the built-in Function.prototype object. By default, it is absent both on that object and along its prototype chain, so the read gives undefined. Use Object.hasOwn(Function.prototype, "prototype") to check that it is not an own property. Modifying shared prototypes can change the lookup result.

The .prototype property is not the internal [[Prototype]] link. In an unmodified environment, Object.getPrototypeOf(Function.prototype) returns Object.prototype, while Object.getPrototypeOf(Ctor) returns Function.prototype for a plain function declared in the same realm. Function.prototype itself is callable: Function.prototype() returns undefined, but new (Function.prototype)() throws a TypeError.

For a plain constructor Ctor that does not return a replacement object, new Ctor() uses Ctor.prototype as the new instance's internal prototype if that property holds an object. Otherwise it uses the constructor's realm's built-in Object.prototype. Reassigning Ctor.prototype does not change the prototype links of existing instances.

The presence of .prototype does not tell you whether new will work: generator functions have this property but are not constructors. A function created with bind can be a constructor if its target is one, even though it initially has no own .prototype property.

Examples

{
  function Ctor() {}
  const instance = new Ctor();

  console.log(Function.prototype.prototype);
  console.log(Object.hasOwn(Function.prototype, "prototype"));
  console.log(Object.getPrototypeOf(Function.prototype) === Object.prototype);
  console.log(Object.getPrototypeOf(Ctor) === Function.prototype);
  console.log(Object.getPrototypeOf(instance) === Ctor.prototype);
}

Result

undefined
false
true
true
true

The first two lines show the lookup result and the absence of an own property. The next three identify the internal prototypes of `Function.prototype`, `Ctor`, and `instance`, respectively.

Related lessons

  • JavaScript · Lesson 5 · Intermediate · Version: 3

Sources