Sign up ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

My JS looks like this:

preLoadImages("/media/img/elements/frontpage/1.jpg","/media/img/elements/frontpage/2.jpg","/media/img/elements/frontpage/3.jpg","/media/img/elements/frontpage/4.jpg","/media/img/elements/frontpage/5.jpg","/media/img/elements/frontpage/6.jpg","/media/img/elements/frontpage/7.jpg","/media/img/elements/frontpage/8.jpg","/media/img/elements/frontpage/9.jpg","/media/img/elements/frontpage/10.jpg");

What would be a better way to do this?

var directory = "/media/img/elements/frontpage/"

preLoadImages(directory+"1.jpg",directory+"2.jpg",directory+"3.jpg",directory+"4.jpg",directory+"5.jpg",directory+"6.jpg",directory+"7.jpg",directory+"8.jpg",directory+"9.jpg",directory+"10.jpg");

Doesn't seem much better. There must be a zip/map/join type thing I can use here?

share|improve this question

2 Answers 2

If the files are all named sequentially, doesn't using a simple loop make sense?

var directory = "/media/img/elements/frontpage/",
    images = [],
    i = 11;

while (--i) {
    images.push(directory + i + ".jpg");
}

preLoadImages(images);

// ...

function preLoadImages(images) {
    console.log("preloading: " + images);
    // TODO: preload stuff
}
share|improve this answer

perhaps you can try apply(), then you can pass in the image paths as an array:

preLoadIamges.apply(this, absoluteFileNames)

then you can build you array in a the way you like, perhaps looking at this unobtrusive implementation of Array.map:

var directory = "/media/img/elements/frontpage/"
var fileNames = ["1.jpg","...","9.jpg"]; 
var absoluteFileNames = fileNames.map(function (value) { 
    return directory + value; 
});
share|improve this answer
    
Good answer, this is probably how I would approach it too. –  Jordan Gray Feb 10 '12 at 14:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.