我已经开始基于数组中的信息创建一系列对象,但后来无法使用数组信息来回调对象。这可能只是让括号正确:
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()
提前感谢您的帮助。
答案 0 :(得分:0)
解决“当名称在字符串中时如何访问变量”问题的常用方法是在对象中构建自己的命名空间:
names =
Opening: whatever_Opening_was_set_to
...
然后您可以按名称访问names
中的值:
total += names[item.name].getClipLengthOutMinusIn()
您也可以使用eval
,但这几乎总是一件可怕的事情。