Javascript 为什么;HTMLDocument==Function.prototype";这是假的吗?
据我所知,Javascript 为什么;HTMLDocument==Function.prototype";这是假的吗?,javascript,html,dom,Javascript,Html,Dom,据我所知,HTMLDocument是一个构造函数,因此我们有以下内容: HTMLDocument.constructor == Function //true HTMLDocument.prototype == document.__proto__ //true HTMLDocument == document.constructor //true 但是为什么当谈到函数.prototype时,它是错误的呢 HTMLDocument.__proto__==Function.prototype /
HTMLDocument
是一个构造函数,因此我们有以下内容:
HTMLDocument.constructor == Function //true
HTMLDocument.prototype == document.__proto__ //true
HTMLDocument == document.constructor //true
但是为什么当谈到函数.prototype
时,它是错误的呢
HTMLDocument.__proto__==Function.prototype // false
document
是创建的实际对象&HTMLDocument
用于构造它。您可以通过在控制台中键入document.constructor
进行检查,控制台中将显示htmldocument
。JavaScript使用原型继承&,内部\uuuuu proto\uuu
属性用于导航原型继承链
它本身是使用函数构造函数
和JavaScript中的任何其他对象构造的。这就是为什么以下语句的计算结果为true
HTMLDocument.constructor == Function // true
但是HTMLDocument
打算用作Document
接口的扩展。因此,任何HTMLDocument
类型的对象
也必须能够访问文档
接口的所有属性。为了保持这一点,htmldocument
的\uuuu proto\uuuu
在内部设置为文档
对象。这就是为什么HTMLDocument.\uuuu proto\uuuu
没有提到函数.prototype
HTMLDocument.__proto__ == Document // true.
HTMLDocument.__proto__ == Function // false
上面的代码片段意味着,HTMLDocument
对象将通过其原型链直接访问Document
对象中的所有属性,而不是函数
对象。你一定已经知道了,这是有明显原因的
这给我们留下了
HTMLDocument.prototype == document.__proto__ // true
上面的语句意味着,document
对象将有权访问其父对象HTMLDocument的
prototype对象,就像所有实例对象一样
为了您的方便,这里是整个继承链:
这是我在查看
HTMLDocument
原型链时得到的输出
Object.getPrototypeOf(HTMLDocument)
// ƒ Document() { [native code] }
Object.getPrototypeOf(Document)
// ƒ Node() { [native code] }
Object.getPrototypeOf(Node)
// ƒ EventTarget() { [native code] }
Object.getPrototypeOf(EventTarget)
// ƒ () { [native code] }
所以
Function
实际上在原型链中,它不是HTMLDocument
的实际直接原型,为什么您希望它是true
?因为任何构造函数函数都等于Function.prototype,比如:Function Foo(){};Foo.\uuuu proto\uuuu==Function.prototype//true
@qqwenti。。。你认为为什么会有一种叫做原型链的东西?@Peter Seliger嗯,我猜JavaScript中的所有东西都遵循原型链规则,每个对象每个函数。。。还有每个html元素。@qqwenti。。。“我猜JavaScript中的所有内容都遵循原型链规则”。。。没错,同时,我也提供了一个完美的解释,解释了为什么你不能期望直接原型(链接)是第一个最好的匹配(在一个链中)。老实说,你的原型链是惊人的!
Object.getPrototypeOf(HTMLDocument)
// ƒ Document() { [native code] }
Object.getPrototypeOf(Document)
// ƒ Node() { [native code] }
Object.getPrototypeOf(Node)
// ƒ EventTarget() { [native code] }
Object.getPrototypeOf(EventTarget)
// ƒ () { [native code] }