Why do built-in functions not have a prototype property?

后端 未结 1 878
[愿得一人]
[愿得一人] 2021-02-08 16:15

Given that the ES 5.1 standard states that...

1) Note at the foot of http://es5.github.com/#x13.2

NOTE A prototype property is automatically created for          


        
相关标签:
1条回答
  • 2021-02-08 16:36

    It's not the .prototype that allows a function to be used as a constructor, but the presence of the [[Construct]] internal method. See this section, step 4.

    Normal functions created by the user script automatically have this internal property set, so all user functions can be called as constructors. This is because the interpreter can't know how the user intends to use that method.

    For native functions the intended usage is known in advance, so the javascript engine can decide which native functions should be callable as constructors. Does it make sense to invoke new [].push?

    It is mentioned in the introductory part to built-in objects that:

    None of the built-in functions described in this clause that are not constructors shall implement the [[Construct]] internal method unless otherwise specified in the description of a particular function. None of the built-in functions described in this clause shall have a prototype property unless otherwise specified in the description of a particular function.

    And the reason, IMHO, is that there is no valid real use case that would need that. There's no good explanation why push should be instantiable: what's the difference between a new push and a new generic object? So, allowing the instantiation of those functions doesn't bring any value to the developer, but it will raise lots of WTFs from others reading the code.

    0 讨论(0)
提交回复
热议问题