[英]Add data to specific object property with index
我有一個看起來像這樣的對象:
imgs: {
img1: {
on: 0,
scr: null
},
img2: {
on: 0,
scr: null
},
and so on...
}
我想要的是使用單擊圖像的索引來達到對象中的特定圖像屬性。
JS:
MoveUpImg: function(index) {
Capsule.imgs['img' + index].on = 1;
// rest of the code
},
該代碼不起作用:
Uncaught TypeError: Cannot set property 'on' of undefined
通過更改為特定的對象屬性,假設img2有效:
Capsule.imgs.img2.on = 1;
...這就是問題所在:
Capsule.imgs['img' + index].on = 1;
(當然,我已經檢查過索引是否為我提供了正確的數字,依此類推)
您正在傳遞img
功能,但嘗試訪問imgs
對象
應該
MoveUpImg: function(imgs, index) {
代替
MoveUpImg: function(img, index) {
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.