文档和文档在JavaScript中有什么区别? [英] What is the difference between Document and document in JavaScript?
问题描述
通过Visual Studio中的智能感知列表(尝试熟悉JavaScript API),我注意到文档
和文档
存在
-
文档
和<$ c $ ? - 什么是
文档
(如果有)的实例? - 如何使用
文档
(因为它不是一个功能,因此不可构造)? - 最重要的是,猴子修补
文件
中的伤害是什么,使其可构建?
这些问题背后的理由是我想创建一些适合我的API的对象(例如; Document
, HTMLElement
等),但是ese似乎已经在某些方面存在,我不确定我应该覆盖他们的本地实现。
文档
和文档
之间有什么区别?
< blockquote>
文档
(或window.document
)是对窗口中包含的文档的引用。 (规格)
Document
是文档的DOM接口,它在全局对象上公开。 (规格, spec )
如何使用
文档
(因为它不是一个函数,因此不可构造)?
这是一个主机对象,不需要遵循EcmaScript规范 - 但这并不意味着它不是一个功能。浏览器也可能不同。然而,它不是要被调用的(如果你尝试它会得到一个
NOT_SUPPORTED_ERR
),还有其他方法来实例化/获取新的文档。您仍然可以使用它是> document $ document
true
> Document.prototype
DocumentPrototype {
adoptNode:Function
构造函数:文档
createAttribute:函数
...
querySelector:函数
querySelectorAll:Function
}
| - NodePrototype
| - 对象
所以你可以扩展其原型,并在您的应用程序中的所有
XMLDocuments
/HTMLDocuments
中使用这些方法(但只有当您知道扩展DOM有什么问题)。
最重要的是,猴子修补
文档
中的伤害是什么,使其可构建?
< blockquote>
我不知道你会怎么做覆盖它可能会损害希望其按上述方式工作的每个脚本(除非您修复新函数的
原型
属性)。也许在某些环境中,c> c c> >
I am currently building an API for JavaScript, predominantly using Visual Studio 2010 and JetBrains WebStorm (brilliant if you're looking for a bulletproof JavaScript IDE).
Whilst looking through the intellisense list in Visual Studio (trying to familiarize myself with the JavaScript API), I noticed that both
Document
anddocument
exist.
- What is the difference between
Document
anddocument
?- What is
document
an instance of (if any) ?- How does one use
Document
(as it is not a function, therefore, not constructable)?- Most importantly, What is the harm in "monkey-patching"
Document
to make it constructable?The rationale behind these questions is that I want to create some objects that fit into my API (for example;
Document
,HTMLElement
etc.), but as these appear to already exist in some respect, I'm not confident that I should be overwriting their native implementation.解决方案What is the difference between
Document
anddocument
?
document
(orwindow.document
) is a reference to the document contained in the window. (spec)
Document
is the DOM interface for documents, which is exposed on the global object. (spec, spec)How does one use
Document
(as it is not a function, therefore, not constructable)?It's a host object and does not need to follow the EcmaScript spec - yet that does not mean it's not a function. It may differ from browser to browser as well. Yet it is not intended to be called (if you try it you'll get a
NOT_SUPPORTED_ERR
), there are other methods to instantiate/obtain new documents. What you can still use it for is> document instanceof Document true > Document.prototype DocumentPrototype { adoptNode: Function constructor: Document createAttribute: Function … querySelector: Function querySelectorAll: Function } |- NodePrototype |- Object
so you could extend its prototype and use those methods on all
XMLDocuments
/HTMLDocuments
in your app (but only if you know what’s wrong with extending the DOM).Most importantly, what is the harm in "monkey-patching"
Document
to make it constructable?I'm not sure how you would do that. Overwriting it could harm every script that expects it to work as above (unless you fix the
prototype
property of your new function). And maybe theDocument
property ofwindow
is non-writable in some environments, so you could harm yourself.这篇关于文档和文档在JavaScript中有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!