I'm trying to create an array of image objects, but struggling. Each object will hold an image and a caption for the image.
The following code works fine when I paste it into Firebug for checking:
Example 1
var imageArray = new Array();
imageArray[0] = new Image();
console.log(imageArray[0]); //result is <img>
imageArray[0].src = "my-image-01.png";
console.log(imageArray[0]); // result is <img src="my-image-01.png"/>
imageArray[0] = {imageCaption: "A caption for the image"}; //an object
console.log(imageArray[0].imageCaption) //result is: A caption for the image
imageArray[1] = new Image()
... etc
However, I thought the following would make more sense, but it keeps throwing an error and I can't understand why.
Example 2
var imageArray = new Array();
imageArray[0]= {
image01: new Image(),
image01.src: "my-image-01.png", //"SyntaxError: missing : after property id"
imageCaption: "An image caption"
};
imageArray[1] = {
image02: new Image().
image02.src: "my-image-02.png",
imageCaption: "Another image caption"
}
Can anyone explain what's wrong with the code above? Is the first example I posted, the approach I should use? Many thanks