*
隐式声明数组。numbers =
*1
*2
*3
相应的javascript代码是:
var numbers;
numbers = [1, 2, 3];
也可以隐式声明对象数组。
tmp =
*name: 'a', id: 1
*name: 'b', id: 2
*name: 'c', id: 3
*name: 'a', id: 4
相应的javascript代码是:
var tmp;
tmp = [{name: 'a', id: 1}, {name: 'b', id: 2}, {name: 'c',
id: 3}, {name: 'a', id: 4}];
<[hello world !]>
对应的javascript代码是:
['hello', 'world', '!'];
tmp.filter -> it.name is 'a'
对应的javascript代码是:
tmp.filter(function(it){
return it.name === 'a';
});
使用之前的tmp数组,得到的结果是:
[{"name":"a","id":1},{"name":"a","id":4}];
tmp.sort (a, b) -> a.name > b.name
相应的javascript代码是:
tmp.sort(function(a, b){
return a.name > b.name;
});
使用之前的tmp数组,得到的结果是:
[{"name":"a","id":1},{"name":"a","id":4},{"name":"b","id":2},{"name":"c","id":3}];
fn = (item) ->
item.score = 100;
item
[fn item for item in tmp]
相应的javascript代码是:
var fn, i$, ref$, len$, item;
fn = function(item){
item.score = 100;
return item;
};
for (i$ = 0, len$ = (ref$ = tmp).length; i$ < len$; ++i$) {
item = ref$[i$];
fn(item);
}
使用之前的tmp数组,得到的结果是:
[{"name":"a","id":1,"score":100},{"name":"b","id":2,"score":100},{"name":"c","id":3,"score":100},{"name":"a","id":4,"score":100}];
参考资料:
Livascript官方文档