如果我不在javascript中的变量中分配新图像会发生什么

时间:2016-04-06 20:47:02

标签: javascript

我正在努力了解新图片。我是否能够创建一个应该保存图像而不为其分配新图像对象的变量?我知道您在创建数组时不必指定新数组,或者新对象创建新对象。是将新图像分配给您将以旧方法保存图像的变量,还是我还需要这样做?

2 个答案:

答案 0 :(得分:1)

new Array() = []new Object() = {}有简写示例,但new Image()没有已知的简写/快捷方式。这实际上是HTML 5规范的一部分,其中此代码来自W3C网站:

image = new Image( [ width [, height ] ] )
  

返回一个新的img元素,其width和height属性设置为   如果适用,在相关参数中传递的值。

请参阅此链接的绿色框(略微向上滚动):https://www.w3.org/TR/html5/embedded-content-0.html#dom-image

因此,Web上的每个JS示例似乎都在代码示例中使用new Image()。但是,你可以使用像这样的&为其分配属性。

var img = document.createElement('img');
img.src = '...';
img.height = 123;
img.width = 456;

它不像“()”那么酷,因为已经采用了这些键盘对:[]{}()。那些是阵列,对象和放大器功能参数分别。这看起来更像是HTML标记:<>。所以键盘上确实没有任何东西可以为new Image()创建一个快捷/简写组。

以下是一些JS简写链接:

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

答案 1 :(得分:0)

@Clomp做了很好的解释。但这是另一个如何使用这种知识的例子。

// 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);

these instructions

相关问题