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

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

问题描述

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



通过Visual Studio中的智能感知列表(尝试熟悉JavaScript API),我注意到文档文档存在


  1. 文档和<$ c $ ?

  2. 什么是文档(如果有)的实例?

  3. 如何使用文档(因为它不是一个功能,因此不可构造)?

  4. 最重要的是,猴子修补文件中的伤害是什么,使其可构建?

这些问题背后的理由是我想创建一些适合我的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 and document exist.

  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?

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 and document?

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

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

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