gpt4 book ai didi

coffeescript - CoffeeScript 循环效率

转载 作者:行者123 更新时间:2023-12-02 00:22:11 28 4
gpt4 key购买 nike

super 简单的coffeescript问题

circles = []
for coordinate, i in coordinates
circles[i] = new MakeCircle(cnBlue, coordinate.x, coordinate.y, 16, 8, 0, theCanvas.ctx)

这行得通。但我知道,使用 candy 语法,可能有甚至更像 CoffeeScript 的方式来编写它。有没有办法不使用 i 来写这个?

最佳答案

规范的 CoffeeScript 方法是使用 for comprehension,它将返回一个数组:

circles = for coordinate in coordinates
new MakeCircle(cnBlue, coordinate.x, coordinate.y, 16, 8, 0, theCanvas.ctx)

或者,在一行中:

circles = (new MakeCircle(cnBlue, coordinate.x, coordinate.y, 16, 8, 0, theCanvas.ctx) for coordinate in coordinates)

参见 Loops and Comprehensions :

Note how because we are assigning the value of the comprehensions to a variable in the example above, CoffeeScript is collecting the result of each iteration into an array.

关于coffeescript - CoffeeScript 循环效率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10443122/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com