[英]What happens if I don't assign new Image in a variable in javascript
I'm trying to get some understanding about new image . 我正在努力使人们对新形象有所了解。 Would I be able to create a variable that is supposed to hold an image without assigning a new image object to it?
我是否可以创建一个应该保存图像的变量,而无需为其分配新的图像对象? I know you don't have to assign new array when creating an array or new object creating a new object.
我知道你没有创建一个数组或新对象创建新对象时,指定新的阵列 。 Is assigning new image to a variable that you are going to hold a image in an old method or do I still need to do it?
是将新图像分配给要使用旧方法保存图像的变量,还是我仍然需要这样做?
There are shorthand examples for new Array()
= []
and new Object()
= {}
, but there is no known shorthand/shortcut for new Image()
. 有
new Array()
= []
和new Object()
= {}
速记示例,但是new Image()
没有已知的速记/快捷方式。 That is actually part of the HTML 5 spec, where this code comes from the W3C website: 这实际上是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.
返回一个新的img元素,并将width和height属性设置为在相关参数中传递的值(如果适用)。
See the green box at this link (scroll up slightly): https://www.w3.org/TR/html5/embedded-content-0.html#dom-image 看到此链接上的绿色框(略微向上滚动): https : //www.w3.org/TR/html5/embedded-content-0.html#dom-image
So every JS example on the web seems to be using new Image()
in the code examples. 因此,网络上的每个JS示例似乎都在代码示例中使用了
new Image()
。 However, you can use longhand, like this & assign properties to it. 但是,您可以像这样使用正手并为其分配属性。
var img = document.createElement('img');
img.src = '...';
img.height = 123;
img.width = 456;
It's not as cool as "()", because these keyboard pairs are already taken: []
, {}
and ()
. 它不像“()”那样酷,因为已经使用了以下键盘对:
[]
, {}
和()
。 Those are for array, object & function parameters, respectively. 这些分别用于数组,对象和函数参数。 This looks more like an HTML tag:
<>
. 这看起来更像HTML标记:
<>
。 So there really isn't anything left on the keyboard, which could create a group for a shortcut / shorthand for new Image()
. 因此,键盘上实际上什么也没有,它可以为
new Image()
的快捷方式/速记创建一个组。
Here are a few JS shorthand links: 以下是一些JS速记链接:
@Clomp does a good explanation. @Clomp很好地解释了。 But here is another example on how to use this knowledge.
但是,这是有关如何使用此知识的另一个示例。
// Create new image element
var image = new Image( 100, 100 );
// Add an image source to element
image.src = 'http://www.publicdomainpictures.net/pictures/130000/velka/blue-tit-bird-clipart.jpg';
// Output new image element to console
console.log(image);
// Add image element to body
$('body').html(image);
https://jsbin.com/medohiquti/edit?html,js,output https://jsbin.com/medohiquti/edit?html,js,输出
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.