我正在尝试对新图像有一些了解。我是否能够创建一个应该保存图像的变量而不为其分配新的图像对象?我知道在创建数组或新对象创建新对象时不必分配新数组。是将新图像分配给您将用旧方法保存图像的变量还是我仍然需要这样做?
最佳答案
有 new Array()
= []
和 new Object()
= {}
的速记示例,但 new Image()
没有已知的速记/快捷方式。这实际上是 HTML 5 规范的一部分,其中代码来自 W3C 网站:
image = new Image( [ width [, height ] ] )
Returns a new img element, with the width and height attributes set to the values passed in the relevant arguments, if applicable.
查看此链接中的绿色框(稍微向上滚动):https://www.w3.org/TR/html5/embedded-content-0.html#dom-image
所以网络上的每个 JS 示例似乎都在代码示例中使用 new Image()
。但是,您可以像这样使用普通方式并为其分配属性。
var img = document.createElement('img');
img.src = '...';
img.height = 123;
img.width = 456;
它不像“()”那么酷,因为这些键盘对已经被占用: []
、 {}
和 ()
。这些分别用于数组、对象和函数参数。这看起来更像是一个 HTML 标签: <>
。所以键盘上确实没有任何东西可以为 new Image()
创建一个快捷方式/简写组。
以下是一些 JS 速记链接:
关于javascript - 如果我不在 javascript 中的变量中分配新图像会发生什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36462012/