Javascript Image object without instantiating
Posted
by user276027
on Stack Overflow
See other posts from Stack Overflow
or by user276027
Published on 2010-02-18T12:34:36Z
Indexed on
2010/04/10
10:03 UTC
Read the original article
Hit count: 111
JavaScript
This question is about javascript performance. Consider 3 examples for illustration:
function loadImgA() {
new Image().src="http://example.com/image.gif"
}
function loadImgA1() {
Image().src="http://example.com/image.gif"
}
function loadImgB() {
var testImg = new Image();
testImg.src="http://example.com/image.gif"
}
Now the point is I don't really need to manipulate the the image object after it was created, hence loadImgA(). The question is, what happens if nothing is assigned to the return value of the new Image() constructor - in that case I can actually skip the 'new' keyword as in loadImgA1()? Does the object then live outside the function or somehow affects memory usage? Other implications, differences? I reckon not, as no real instance was actually created?
To put this into perspective, I only need to get the http request for image through. No preloading or other advanced image manipulation. What would be the preferred method from the above?
© Stack Overflow or respective owner