0

可能这是一个noobisch问题。目前我正在摆弄 Framer.js。我有一个 CoffeeScript 问题;

types = ["orange", "apple", "banana", "grapefruit", "pear"]
for i in types
    li = new TextLayer
        text: types
        y: li * 60
    li.parent = dropdownList

    print "list-item-" + "#{i}", i

所以我有一个数组,我想向一个对象实例声明一个动态变量。上面的代码只生成 5 个 li 层(这是 Framer 特定的 > 我不想在编辑器中使用非自我解释的层名称)

所以在for循环中;

var item-orange = 新层...

var item-apple = new Layer... 等等

我怎么能用 CoffeeScript 完成这个?

4

1 回答 1

0

我不确定,但我认为您要做的是按名称获取对每个创建层的引用,对吗?您可以通过将它们存储在引用其名称的对象中来做到这一点:

types = ["orange", "apple", "banana", "grapefruit", "pear"]

# Create an empty layers object, outside of the loop
layers = {}

# Loop over the types array, storing the type in the variable named type
# And the number of the current loop in the variable index
for type, index  in types
    li = new Layer
        html: type
        y: index * 220
        name: "list-item-#{type}"
    # Store the layer in the layer object under the type key
    layers[type] = li 

    print "list-item-" + "#{type}", layers[type]

# Get a specific layer out of the layers array
layers['apple'].animate
    x: 300

完整的例子在这里:http ://share.framerjs.com/5owxrbz5hqse/

于 2016-11-03T08:33:19.277 回答