简体   繁体   English

如果我没有在javascript中的变量中分配新的图片,该怎么办

[英]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速记链接:

  1. http://www.sitepoint.com/shorthand-javascript-techniques/ http://www.sitepoint.com/shorthand-javascript-techniques/
  2. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Assignment_Operators

@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.

相关问题 如果我不 removeEventListener (JavaScript) 会发生什么? - What happens if I don't removeEventListener (JavaScript)? 如果新实例对象未分配给变量会发生什么? - What happens if the new instance object is not assign to a variable? 如果我不在 Javascript 函数中传递参数会发生什么? - What happens if I don't pass a parameter in a Javascript function? 分配不存在的javascript变量会发生什么 - what happens to a javascript variable assign with a non existent variable Javascript:没有名字的对象会发生什么? - Javascript: What happens to objects that don't have a name? 如果我没有在.then()函数中传递参数会发生什么,它将返回什么 - What happens if i don't pass a parameter in .then() function, what will it return 当我在javascript中'Object.assign`到原始类型时会发生什么? - What happens when I `Object.assign` to a primitive type in javascript? 在JavaScript中,如果我分配给具有getter但没有setter的对象属性,会发生什么? - In JavaScript, what happens if I assign to an object property that has a getter but no setter? 如果我不测试 Passive 事件侦听器支持会怎样? - What happens if I don't test Passive event listeners support? 在JavaScript中分配undefined会发生什么? - What happens when you assign undefined in JavaScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM