在 CoffeeScript 中向 Array 追加值的规定方法是什么?我已经查看过 PragProg CoffeeScript 书籍,但它只讨论创建、切片和拼接,以及迭代,而没有附加。
Good old push still works.
push
x = [] x.push 'a'
Far better is to use list comprehensions.
For instance rather than this:
things = [] for x in list things.push x.color
do this instead:
things = (x.color for x in list)
If you are chaining calls then you want the append to return the array rather than it's length. In this case you can use .concat([newElement])
Has to be [newElement] as concat is expecting an array like the one its concatenating to. Not efficient but looks cool in the right setting.