1

I have a function through which I pass some variables as arguments. I need some of those to become variable names or object property names.

key(500, 'door', 10, 'bounceOut', 'regY', -150, 5, 'bounceIn');
function key(point, instance, speed, ease, prop, value, speed2, ease2, prop2, value2){
        ani.instance = true;
        createjs.Tween.get(instance, {override: true}).to({prop: value}, -(value + -instance.prop) * speed, createjs.Ease.ease);
    }

The solution stated here should have worked, but it does nothing for me.

 // ani.instance changed to:
    ani[instance]; // no error here
    // instance.prop changed to:
    instance[prop]; // returns 'undefined', expected it to return 'door[regY]'. instance and prop return 'door' and 'regY' respectively though

What is going on here? Can anyone nudge me in the right direction? Thank you :)

EDIT What I want the code to read is:

door.regY = 200;
createjs.Tween.get(door, {override: true}).to({regY: -150}, -(-150 + -door.regY) * 10, createjs.Ease.bounceOut);
Community
  • 1
  • 1
AKG
  • 2,936
  • 5
  • 27
  • 36
  • Is the intention of `{prop: value}` to create a property with a name `regY`? If so you need to `var O = {}; O[prop] = value;` and pass `O` – Alex K. Jun 24 '13 at 16:43
  • @AlexK. Not quite. Outside of this function, `door.regY` has already been defined. This particular bit `{prop: value}` reacts on `get(instance, {override: true})`. I want it to read `get(door, {override: true}).to({door: regY})` etc. – AKG Jun 24 '13 at 16:46

2 Answers2

2

In ES5 , You can only create or fetch "dynamic properties" on an object . So you need an object , or use this. with the [] notation.

var prop = "something";

this[prop] ="my value";

//this.something=== "my value"

var obj = {};

obj[prop] = "a value";

// obj.something === "a value";

var obj1 = {prop:"value"} 
// will not make obj.something === "value" 
// but obj.prop==="value" because in this case prop cannot be a variable

so you need to do something like that :

var arg1 = {};
arg1[prop]=value2;
var arg2 = {}
arg2[prop] = value2;
createjs.Tween.get(instance, arg1).to(arg2, -(value2 + -door[prop]) * 10, createjs.Ease.ease);

and so on , you need to define the objects before passing them to createjs.Tween.get method.

instance probably needs to be an object and not a string but since i know neither createJS or your script i cant tell.

EDIT : read this : http://www.jibbering.com/faq/faq_notes/square_brackets.html

mpm
  • 20,148
  • 7
  • 50
  • 55
1

In the invokation of your key function, you're passing a string as the instance argument. Thus, you will be able to access properties of the string, not your door object.

I assume there's a door object since you probably have such an object somewhere else, which has the property you're trying to access inside key (regY in your example). You should be calling key like this, then:

key(500, door, 10, 'bounceOut', 'regY', -150, 5, 'bounceIn');

where door is a variable referencing that other object. Of course, you wouldn't know the variable name inside key then. If you also need that, you'll need to pass it as a different parameter. However, it doesn't sound like a good decision, do you actually need it?

Esteban
  • 2,540
  • 21
  • 27
  • thanks for your reply. I'm trying my best to follow, because I'm really a beginner at JavaScript and trying to learn. So I cannot pass a literal string to the function? (`instance`, `prop` etc. acting as placeholders) – AKG Jun 24 '13 at 17:01
  • 1
    If you pass a string, you need somewhere to get the actual object (`door`) from, using that string. Let's say you have all your `instance`s stored in an object called instanceStorage, you could get your instance by name like this (inside `key`): `instance = instanceStorage[instance];`. Notice you'll lose your string value, you're overwriting it with the actual object (you could use a different variable name to keep both). However, if you don't have somewhere to get your instance from by name, you won't be able to do what you want, you'll just have to pass the actual instance to `key`. – Esteban Jun 24 '13 at 17:08
  • I recommend the [Eloquent Javascript](http://eloquentjavascript.net/contents.html) book to get a hold of JavaScript's syntax and semantics. There are also excellent (mostly more advanced) articles at (Superhero.js)[http://superherojs.com/]. – Esteban Jun 24 '13 at 17:11