`new Image()`和`document.createElement('img')`之间有区别吗? [英] Is there a difference between `new Image()` and `document.createElement('img')`?

查看:259
本文介绍了`new Image()`和`document.createElement('img')`之间有区别吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在javascript中,我可以做:

In javascript, I can do:

img1 = new Image();
img2 = document.createElement('img');

我的问题是,两种方法有区别吗?我已经阅读了 Image Form 元素被称为主机对象,是这样吗?如果是,什么是主机对象?

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的一部分,而< a href =http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html#ID-2141741547 =noreferrer> document.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级别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第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 元素被称为主机对象,是这样吗?

I've read somewhere that Image, Form, and Element is called host objects, is this true?

是的。


如果是,什么重新托管对象?

If it is, what are host objects?

ECMAScript规范以这种方式激励主机对象:

The ECMAScript specification motivates host objects this way:


ECMAScript是一种用于执行计算的面向对象编程语言并操纵主机环境中的计算对象。这里定义的ECMAScript不是计算自给自足的;实际上,在本说明书中没有规定外部数据的输入或计算结果的输出。相反,预期ECMAScript程序的计算环境不仅将提供本说明书中描述的对象和其他设施,还将提供特定的环境特定主机对象,其描述和行为超出了本规范的范围,除了指出它们可以提供可以访问的某些属性以及可以从ECMAScript程序调用的某些功能。

ECMAScript is an object-oriented programming language for performing computations and manipulating computational objects within a host environment. ECMAScript as defined here is not intended to be computationally self-sufficient; indeed, there are no provisions in this specification for input of external data or output of computed results. Instead, it is expected that the computational environment of an ECMAScript program will provide not only the objects and other facilities described in this specification but also certain environment-specific host objects, whose description and behaviour are beyond the scope of this specification except to indicate that they may provide certain properties that can be accessed and certain functions that can be called from an ECMAScript program.


对象
由主机环境提供的
对象,以完成ECMAScript的执行环境。

注意任何非本机的对象都是主机对象。

host object
object supplied by the host environment to complete the execution environment of ECMAScript.
NOTE Any object that is not native is a host object.

所以在规范中未定义并由环境提供的任何对象都是一个主机对象。这些例如在浏览器(其中包括)中:窗口文档控制台

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')`之间有区别吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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