On Aug 17, 11:49 am, 0m4r <omar.adob...@gmail.com> wrote:
> On Aug 16, 4:50 pm, Marcos Toledo <mtol...@gmail.com> wrote:
>
>
>
> > Hi Omar, Rob,
>
> > This question actually got me thinking a lot. Although I agree with
> > Rob in the details about your HTML (div closing, languague, etc), and
> > all the discouraging about extending the "Image" DOM Object, I think
> > the question has bigger merits, even if theoretically, which got me
> > thinking as well.
>
> > Your actual result contain only a single element inside the test div,
> > instead of the expected 2. And it is, apparently, only the second
> > element, but I noticed something:
>
> > When you define "Image" as your prototype, by the time you instantiate
> > the first Image, all of the attributes your object has are the
> > attributes found in the Image prototype. This means that, by the time
> > you are assining this.src the value in the parameter, this is an
> > actual image.
>
> > When you create your second instance, with "Image2.jpg", due to the
> > prototype chain, you also have all of the attributes found in the
> > Image prototype assined the this. Though, the weird part is:
>
> > *The src attribute found when creating the second image, is
> > "Image1.jpg"*
>
> > This means that, internally, the 1st object and the 2nd object created
> > through your constructors are actually the same. The src in the 1st is
> > overriden with the 2nd, and the appendChild fails, because the object
> > has already been appended.
>
> This is what I was supposing too... You are confirming my thoughts!
>
> > This wouldn't happen if you mimicked all of the DOM Image object's
> > attributes and assigned it to another non-dom object, say, Image2
> > {src:''};, and added that new object to the prototype instead of
> > Image.
>
> How can I do this? I can't figure out how to reproduce the behavior
> you are talcking about.
> could you pleas wrote me a simple snippet?
>
> anyway, thanks for your answer!
>
> Omar
I just use this function:
//START
function downloadImage()
{
for(var i = 0, argumentsLength = arguments.length, newImages = []; i
< argumentsLength; i++)
{
newImages[i] = new Image;
newImages[i].src = arguments[i];
}
}
//END
So you can call as many as you wish. For example:
//START
downloadImage('logo.gif'); // just one
downloadImage('logo.gif', 'example.jpg', 'footer.png'); // let's have
a few
//END
Apekatthjerne
|