我想創建一個圖像對象的數組,但掙扎。每個對象都會保存圖像和圖像的標題。創建一個圖像對象數組
下面的代碼工作正常,當我把它粘貼到螢火蟲的檢查:
例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()
...等
但是,我認爲以下將使更多感覺,但它一直拋出一個錯誤,我不明白爲什麼。
例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"
}
誰能解釋一下什麼錯上面的代碼?我發佈的第一個例子,我應該使用的方法?非常感謝
非常感謝。我沒有意識到我可以將.imageCaption附加到數組名稱以使其工作。現在看起來很簡單:-) –