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).
-
2I don't know if the "why" is relevant, it just is, but here's what the spec says ecma-international.org/ecma-262/7.0/…elclanrs– elclanrs2016年07月16日 12:55:04 +00:00Commented Jul 16, 2016 at 12:55
-
Perhaps the NOTE in that section explains why Function.prototype is a function at allJaromanda X– Jaromanda X2016年07月16日 13:08:04 +00:00Commented Jul 16, 2016 at 13:08
-
1This was part of Function.prototype is a function, but probably that should have been split into multiple questions, so I don't think this should be closed as duplicate.Oriol– Oriol2016年07月16日 13:33:15 +00:00Commented Jul 16, 2016 at 13:33
2 Answers 2
Only constructors have the prototype property:
Function instances that can be used as a constructor have a
prototypeproperty.
There are multiple examples of non-constructor functions apart from Function.prototype, such as
Methods in
Mathobject:typeof Math.pow; // "function" 'prototype' in Math.pow; // falseSome host objects:
typeof document.createElement('object'); // "function" 'prototype' in document.createElement('object'); // falseIn ES6, arrow functions:
typeof (x => x * x); // "function" 'prototype' in (x => x * x); // false
2 Comments
prototype own property as the spec appears to leave room for? (section 9.3 para 6: ecma-international.org/ecma-262/7.0/…)prototype property if the function is not a constructor.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".