Arrays 基于数组中的信息访问先前创建的对象的Coffeescript
我已经开始根据数组中的信息创建一系列对象,但后来无法使用数组信息调用对象。这可能只是正确使用括号的问题:Arrays 基于数组中的信息访问先前创建的对象的Coffeescript,arrays,object,coffeescript,Arrays,Object,Coffeescript,我已经开始根据数组中的信息创建一系列对象,但后来无法使用数组信息调用对象。这可能只是正确使用括号的问题: total = 0 for item in @timelineArray # This prints the object that was previously created console.log Opening # This prints the clip length for that object console.log Opening.getC
total = 0
for item in @timelineArray
# This prints the object that was previously created
console.log Opening
# This prints the clip length for that object
console.log Opening.getClipLengthOutMinusIn()
# This prints the word Opening which is the first item in the timeline array
console.log item.name
# This yields an error "TypeError: Object Opening has no method 'getClipLengthOutMinusIn'"
total += (item.name).getClipLengthOutMinusIn()
提前感谢您的帮助。解决“当变量名称在字符串中时如何访问变量”问题的常用方法是在对象中构建自己的命名空间:
names =
Opening: whatever_Opening_was_set_to
...
然后您可以按名称访问名称中的值:
total += names[item.name].getClipLengthOutMinusIn()
您也可以使用,但这几乎总是一件可怕的事情。所以您在范围中有一个变量开始
,然后是项。name
将是该变量的名称(开始
)?