2

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).

asked Jul 16, 2016 at 12:49
3
  • 2
    I 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/… Commented Jul 16, 2016 at 12:55
  • Perhaps the NOTE in that section explains why Function.prototype is a function at all Commented Jul 16, 2016 at 13:08
  • 1
    This 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. Commented Jul 16, 2016 at 13:33

2 Answers 2

2

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
    
answered Jul 16, 2016 at 13:29
Sign up to request clarification or add additional context in comments.

2 Comments

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: ecma-international.org/ecma-262/7.0/…)
@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.
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".

answered Jul 16, 2016 at 13:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.