views:

42

answers:

2

I want to use the value of a variable as an "index" of an object, an of a value inside of it. Unfortunately this code won't run.


            animatedObjects = {
                userPosition.uid: {
                    "uid": userPosition.uid,
                    "x": 30,
                    "y": 31
                }
            }

Where is it going wrong?

+2  A: 

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}
Peter
Indeed, I must have done something wrong with userPosition.uid, because that method now does work.
skerit
A: 
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).

seanizer