If X.prototype
has Y.prototype
as its [[Prototype]] object,
Then X
has Y
as its [[Prototype]] object
不,这不是一个有效的概括.这很常见,但并不是无处不在.这种模式是在ES6class
es通过原型链引入static
个属性(特别是方法)的继承之后才开始出现的.在此之前,构造函数都是从Function.prototype
继承而来的纯函数.许多图书馆仍在这样做.
然而,这种普遍性适用于几乎所有的内置类,包括本机类(由ECMAScript定义)和主机提供的类(如DOM).对于the Error
hierarchy,这是有意识地在ES6中引入的,在ES5.1中,the native error constructors仍然继承自Function.prototype
.
对于Web API,其接口的Web IDL规范prescribes just this pattern:
- Let 100 be 101.[[Intrinsics]].[[%Function.prototype%]].
- 一百零五
和
- […]if 102 is declared to inherit from another interface, then set 103 to the 100 in realm of that 101..
- […]
- 一百零二
看看这些规则,实际上还有第二个例外:Window
构造函数继承自EventTarget
,但Window.prototype
继承自WindowProperties
对象(后者又继承EventTarget.prototype
).
为什么DOMException
构造函数本身不能将Error
构造函数作为其[[Prototype]],还有更深层次的原因吗?
不是的.不过,有几个可能的解释:
- 它太老了,
DOMException
已经是第一个浏览器js实现的一部分了
没人更新
- 它与网络不兼容,无法进行更新
这只是一个疏忽
- 它已经更新了,但浏览器还没有实现它
Edit: Actually Web IDL explicitly describes it as an exception to the rule. It does not inherit
from Error
, but it still "has its [[Prototype]] internal slot set to the intrinsic object %Error.prototype%
". This was discussed in issue #55 和 changed in PR #378 to align with the implementation reality - away from the previously described [[Prototype]] value of %Error%
.