I want to use the value of a variable as an "index" of an object, an of a value inside of it. Unfortu开发者_StackOverflownately this code won't run.
animatedObjects = {
userPosition.uid: {
"uid": userPosition.uid,
"x": 30,
"y": 31
}
}
Where is it going wrong?
userPosition.uid
is probably not working. You're also missing a closing brace.
Here's a minimal example which shows the concept working. If you have userPosition.uid
working, your example is all good. Check you can output just userPosition.uid
.
a = {}
b = {c: 3}
a[b.c] = 5
a // now equal to {3: 5}
animatedObjects = {
userPosition.uid: { // pos 1
"uid": userPosition.uid, // pos2
"x": 30,
"y": 31
}
}
This can't work. First of all, in pos 1, you probably mean
animatedObjects = {
userPosition:{
uid :
}
}
otherwise you're creating animatedObjects['userPosition.uid']
instead of animatedObjects.userPosition.uid
.
Second, in pos 2, you are trying to initialize a variable with the object your just creating. This can't work, as there is no reference to pass. (There is no this
context in a plain object, you can only pass them by reference when they are created, but not during creation).
精彩评论