Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/459.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么;HTMLDocument==Function.prototype";这是假的吗?_Javascript_Html_Dom - Fatal编程技术网

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] }