1

In the function below, I want to pass two arguments. instance refers to an object, prop refers to an object property name.

door.x = 20; // door['x'] refers to the same
key(door, 'x');
function key(instance, prop) {
    Tween.get(instance, {override: true}).to({prop: -150}, instance[prop]);
}

Because I need to be able to refer to door['x'] at one point (which is another way of writing door.x), x always need to be a string. However, the same x here needs to be used here as an object property name, but I can't have a string there, because the code would not work:

Tween.get(door, {override: true}).to({'x': -150}, door['x']); // does not work because a string has been passed as an object property name

What I really want is this:

Tween.get(door, {override: true}).to({x: -150}, door['x']); // works

So, my question is: is there some sort of method which allows me to 'unstring' a string? Or is there perhaps any other solution around this?

4

1 回答 1

3

Something like this?

function key(instance, prop) {
    var obj = {}; 
    obj[prop] = -150; 
    Tween.get(instance, {override: true}).to(obj, instance[prop]);
}
于 2013-06-25T20:53:27.957 回答