为什么内置函数没有原型属性? [英] Why do built-in functions not have a prototype property?

查看:89
本文介绍了为什么内置函数没有原型属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

鉴于ES 5.1标准规定......

Given that the ES 5.1 standard states that...

1)注意http://es5.github.com/#x13.2

NOTE A prototype property is automatically created for every function,
to allow for the possibility that the function will be used as a constructor.

2) http://es5.github.com/#x15.3.5.2

NOTE Function objects created using Function.prototype.bind do not have
a prototype property.

(这意味着所有其他功能都有)

(which implies that all other functions do)

...为什么内置函数不再具有原型属性?:

...why do built-in functions no longer have a prototype property?:

[].push.prototype; //undefined
Math.max.prototype; //undefined

此外,即使为这些内置函数分配了一个原型属性,这些内置函数也不能用作构造函数:

Moreover these built-ins cannot be used as constructors even when they are assigned a prototype property:

[].push.prototype = {};
[].push.prototype; //[object Object]
new [].push(); //TypeError: function push() { [native code] } is not a constructor

相反,删除来自用户定义对象的prototype属性仍然允许它用作构造函数,并且实际上将通用对象分配给生成的实例的[[prototype]]:

Conversely, removing the prototype property from a user defined object still allows it to be used as a constructor, and in fact assigns a generic object to the [[prototype]] of the generated instances:

var A = function() {};
A.prototype = undefined;
A.prototype; //undefined
(new A()).__proto__; //[object Object]

内置函数现在是子类型的构造函数还是函数?

Are built in functions now sub-typed as either constructors or functions?

[在大多数现代浏览器中测试]

[Tested in most modern browsers]

推荐答案

这不是 .prototype 允许将函数用作构造函数,但是 [[Construct]] 内部方法。请参见此部分,第4步。

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.

对于本机函数,预先知道预期用法,因此javascript引擎可以决定哪些本机函数应该可以作为构造函数调用。调用 new [] .push 是否有意义?

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?

内置对象的介绍部分


除非在特定函数的描述中另有规定,否则本节中描述的非构造函数的内置函数都不应实现[[Construct]]内部方法。除非在特定函数的描述中另有规定,否则本节中描述的任何内置函数都不应具有原型属性。

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.

恕我直言,原因是没有有效的实际用例需要它。没有很好的解释为什么 push 应该是可实例化的:新的 push 与新的通用对象之间有什么区别?因此,允许实例化这些函数不会给开发人员带来任何价值,但它会引发很多 WTF

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.

这篇关于为什么内置函数没有原型属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆