JavaScript 中的 Document 和 document 有什么区别? [英] What is the difference between Document and document in JavaScript?

查看:63
本文介绍了JavaScript 中的 Document 和 document 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为 JavaScript 构建 API,主要使用 Visual Studio 2010 和 JetBrains WebStorm(如果您正在寻找防弹的 JavaScript IDE,那将非常棒).

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).

在 Visual Studio 中查看智能感知列表(试图让自己熟悉 JavaScript API)时,我注意到 Documentdocument 都存在.

Whilst looking through the intellisense list in Visual Studio (trying to familiarize myself with the JavaScript API), I noticed that both Document and document exist.

  1. Documentdocument 有什么区别?
  2. 什么是document(如果有)的实例?
  3. 如何使用 Document(因为它不是函数,因此不可构造)?
  4. 最重要的是,猴子补丁"Document 使其可构建有什么危害?
  1. What is the difference between Document and document?
  2. What is document an instance of (if any) ?
  3. How does one use Document (as it is not a function, therefore, not constructable)?
  4. Most importantly, What is the harm in "monkey-patching" Document to make it constructable?

这些问题背后的基本原理是我想创建一些适合我的 API 的对象(例如;DocumentHTMLElement 等),但是当这些出现时在某些方面已经存在,我不相信我应该覆盖他们的原生实现.

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.

推荐答案

Documentdocument 有什么区别?

document(或 window.document) 是对窗口中包含的文档的引用.(规范)

Document 是文档的 DOM 接口,暴露在全局对象上.(规范规范)

Document is the DOM interface for documents, which is exposed on the global object. (spec, spec)

如何使用Document(因为它不是函数,因此不可构造)?

How does one use Document (as it is not a function, therefore, not constructable)?

它是一个宿主对象,不需要遵循 EcmaScript 规范——但这并不意味着它不是一个函数.它也可能因浏览器而异.然而它并不打算被调用(如果你尝试它,你会得到一个 NOT_SUPPORTED_ERR),还有其他方法来实例化/获取新文档.你仍然可以使用它的是

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

因此您可以扩展其原型并在您的应用程序中的所有 XMLDocuments/HTMLDocuments 上使用这些方法(但前提是您知道 扩展 DOM 有什么问题).

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).

最重要的是,猴子补丁"Document 使其可构建有什么危害?

Most importantly, what is the harm in "monkey-patching" Document to make it constructable?

我不确定你会怎么做.覆盖它可能会损害希望它按上述方式工作的每个脚本(除非您修复了新函数的 prototype 属性).也许 windowDocument 属性在某些环境中是不可写的,所以你可能会伤害自己.

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 the Document property of window is non-writable in some environments, so you could harm yourself.

这篇关于JavaScript 中的 Document 和 document 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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