I have an object that looks like this:
我有一个看起来像这样的对象:
imgs: {
img1: {
on: 0,
scr: null
},
img2: {
on: 0,
scr: null
},
and so on...
}
What I want is to use the index of a clicked image to reach the specific image property in the object.
我想要的是使用单击图像的索引来到达对象中的特定图像属性。
js:
JS:
MoveUpImg: function(index) {
Capsule.imgs['img' + index].on = 1;
// rest of the code
},
This code doesn't work:
此代码不起作用:
Uncaught TypeError: Cannot set property 'on' of undefined
By changing to a specific object property, let's say img2 it works:
通过更改为特定的对象属性,让我们说img2它的工作原理:
Capsule.imgs.img2.on = 1;
...this means that it's something with this line that's the problem:
...这意味着这条线就是问题所在:
Capsule.imgs['img' + index].on = 1;
(of course I've checked that index gives me the correct number and so on)
(当然我已经检查过索引给我正确的数字等等)
1 个解决方案
#1
1
You are passing img
to function but try to access the imgs
object
您正在将img传递给函数但尝试访问imgs对象
Should be
应该
MoveUpImg: function(imgs, index) {
instead of
代替
MoveUpImg: function(img, index) {
#1
1
You are passing img
to function but try to access the imgs
object
您正在将img传递给函数但尝试访问imgs对象
Should be
应该
MoveUpImg: function(imgs, index) {
instead of
代替
MoveUpImg: function(img, index) {