1

Is Function.prototype the only function without a prototype property?

Why is the property absent instead of having a prototype property with a value of null.

document.write(Object.getOwnPropertyNames(Function.prototype));

Edit: presumably the prototype property is elided because it does not have a [[Construct]] internal method (it is not a constructor).

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • 2
    I don't know if the "why" is relevant, it just is, but here's what the spec says http://www.ecma-international.org/ecma-262/7.0/index.html#sec-properties-of-the-function-prototype-object – elclanrs Jul 16 '16 at 12:55
  • Perhaps the NOTE in that section explains why Function.prototype is a function at all – Jaromanda X Jul 16 '16 at 13:08
  • 1
    This was part of [Function.prototype is a function](http://stackoverflow.com/q/32928810/1529630), but probably that should have been split into multiple questions, so I don't think this should be closed as duplicate. – Oriol Jul 16 '16 at 13:33

2 Answers2

2

Ah, just found that section 9.3 para 6 says:

Built-in functions that are not constructors do not have a prototype property unless otherwise specified in the description of a particular function.

All "normal" functions have the [[Construct]] internal method (section 9.2.3):

If functionKind is "normal", let needsConstruct be true.

Exotic built-in functions may or may not have the [[Construct]] internal method and if they do not, then they do not have the prototype property, "unless otherwise specified".

Ben Aston
  • 53,718
  • 65
  • 205
  • 331
1

Only constructors have the prototype property:

Function instances that can be used as a constructor have a prototype property.

There are multiple examples of non-constructor functions apart from Function.prototype, such as

  • Methods in Math object:

    typeof Math.pow; // "function"
    'prototype' in Math.pow; // false
    
  • Some host objects:

    typeof document.createElement('object'); // "function"
    'prototype' in document.createElement('object'); // false
    
  • In ES6, arrow functions:

    typeof (x => x * x); // "function"
    'prototype' in (x => x * x); // false
    
Oriol
  • 274,082
  • 63
  • 437
  • 513
  • Do you know of any exotic non-constructor functions with the `prototype` own property as the spec appears to leave room for? (section 9.3 para 6: http://www.ecma-international.org/ecma-262/7.0/index.html#sec-built-in-function-objects) – Ben Aston Jul 16 '16 at 13:44
  • 1
    @BenAston I don't know any native example. I don't think there is any point in having a `prototype` property if the function is not a constructor. – Oriol Jul 16 '16 at 14:44