I have an array of images, that I got from facebook. In a for loop I assign an element of the array to a new variable. I then change the variables source to a different one, but with it the source of the arrays element changes too.
function getImages(){
var images = document.querySelectorAll('div.userContentWrapper img');
return images;
}
function getImageHeight(image){
return image.height;
}
function getImageWidth(image){
return image.width;
}
function replaceImages(images) {
var baseImageUrl, height, width, image;
baseImageUrl = 'http://placekitten.com/g/';
for (var i=0, len=images.length; i<len; i++){
image = images[i];
height = getImageHeight(image);
width = getImageWidth(image);
image.src = baseImageUrl + width + '/' + height;
}
}
I don't understand why does the element images[i]
change when we change image.src
If I had any say in this, I would have put images[i].src = image.src;
at the end of the loop, but it seems like that isn't necessary.
Maybe someone can recommend what I should read up to understand this issue better.
Also, is it necessary/advisable to create so many small functions?
This is my first question here, so I apologize, if I wasn't clear enough or the question isn't good enough