问题描述
在javascript中,我可以:
In javascript, I can do:
img1 = new Image();
img2 = document.createElement('img');
我的问题是,这两种方法有区别吗?我在某处读到 Image
、Form
和 Element
被称为 宿主对象,这是真的吗?如果是,什么是宿主对象?
my question is, is there a difference between the two approach? I've read somewhere that Image
, Form
, and Element
is called host objects, is this true? If it is, what are host objects?
哪种方法更可取?
推荐答案
我找不到任何详细的参考资料,但基于 MDC - HTMLImageElement
示例,似乎 Image
是 DOM 级别 0 的一部分,而文档.createElement
是 DOM 级别 2 的一部分.
I couldn't find any detailed reference but based on the comment in the MDC - HTMLImageElement
example, it seems that Image
is part of DOM level 0 whereas document.createElement
is part of DOM level 2.
DOM level 0 是由 Netscape 发明的,它提供了一种访问网站某些元素的方法.基本上所有浏览器都支持它以向后兼容.
但老实说,我不明白为什么 Image
构造函数存在于那里,因为据我所知,没有办法操纵 DOM 级别为 0 的文档.也许它只是在浏览器内部用于创建对象.
DOM level 0 was invented by Netscape and provided a way to access the certain elements of the website. Basically all browsers support it for backwards compatibility.
But to be honest, I don't understand why the Image
constructor exists there, because, as far as I understood it, there was no way to manipulate the document with DOM level 0. Maybe it was only used internally by the browser to create the objects.
DOM level 2 是 W3C 制定的官方标准.
DOM level 2 is an official standard developed by the W3C.
有关 DOM 级别的更多信息,请查看 quirksmode.org -0 级 DOM 和 维基百科.
For more information about the DOM levels, have a look at at quirksmode.org - Level 0 DOM and Wikipedia.
我在某处读到Image
、Form
和Element
被称为宿主对象,这是真的吗?
是的.
如果是,宿主对象是什么?
ECMAScript 规范以这种方式激励宿主对象:
The ECMAScript specification motivates host objects this way:
ECMAScript 是一种面向对象的编程语言,用于在宿主环境中执行计算和操作计算对象.此处定义的 ECMAScript 并非旨在计算自给自足;实际上,本规范中没有关于外部数据输入或计算结果输出的规定.相反,期望 ECMAScript 程序的计算环境不仅会提供本规范中描述的对象和其他设施,还会提供某些特定于环境的宿主对象,这些对象的描述和行为超出本规范的范围,除非表明它们可能提供某些可以访问的属性和可以从 ECMAScript 程序调用的某些函数.
和
宿主对象
宿主环境提供的对象,完成ECMAScript的执行环境.
注意任何非本机对象都是宿主对象.
因此,任何未在规范中定义并由环境提供的对象都是宿主对象.例如在浏览器中(以及其他):window
、document
和 console
.
So any object that is not defined in the specification and provided by the environment is a host object. These are for example in a browser (among others): window
, document
and console
.
这篇关于`new Image()` 和 `document.createElement('img')` 之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!