简体   繁体   English

动态对象名称属性不起作用

[英]Dynamic object name property not working

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);

In ES5 , You can only create or fetch "dynamic properties" on an object . 在ES5中,您只能在对象上创建或获取“动态属性”。 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. 等等,您需要在将对象传递给createjs.Tween.get方法之前定义它们。

instance probably needs to be an object and not a string but since i know neither createJS or your script i cant tell. instance可能需要是一个对象而不是一个字符串,但因为我既不知道createJS,也不知道你的脚本。

EDIT : read this : http://www.jibbering.com/faq/faq_notes/square_brackets.html 编辑:阅读: http//www.jibbering.com/faq/faq_notes/square_brackets.html

In the invokation of your key function, you're passing a string as the instance argument. 在您的key函数的调用中,您将传递一个字符串作为instance参数。 Thus, you will be able to access properties of the string, not your door object. 因此,您将能够访问字符串的属性,而不是door对象的属性。

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). 我假设有一个door对象,因为你可能在其他地方有这样一个对象,它有你试图在key内部访问的属性(在你的例子中是regY )。 You should be calling key like this, then: 你应该像这样调用key ,然后:

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

where door is a variable referencing that other object. 其中door是引用其他对象的变量。 Of course, you wouldn't know the variable name inside key then. 当然,你不会知道key内的变量名。 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? 但是,这听起来不是一个好的决定,你真的需要吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM