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

有些话、适合烂在心里 提交于 2019-12-03 07:24:08

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!