您的位置:首页 > 其它

一个简单的存储过程

2010-11-29 12:36 183 查看
The Little Book on CoffeeScript official site
引用 CoffeeScript is a little language that compiles down to JavaScript. The syntax is inspired by Ruby and Python, and implements many features from those two languages.
I found some amazing features when using CoffeeScript on my project. Here I have some examples.
wonderful idioms of array and object
Array#forEach() Array#map() Array#filter() are latest features introduced in ECMAScript 5, CoffeeScript have compatible idioms to implement them.
# forEach function
myFunction(item) for item in array

# map function
result = (item.name for item in array)

# filter and map
result = (item for item in array when item.name is "test")

Demo: convert objects array to values array using coffeescript online demo
# convert object to array in order
objs = [ {
sn: "DFJJK"
project: "fly"
department: "origin"
feelings :"well"
},{
sn: "DFJJK"
project: "fly"
department: "origin"
feelings :"well"
}]

order = ["project", "sn",  "feelings", "department"]

result =
for obj in objs
for name in order
obj[name]

# or in simple way
#result = ((obj[name] for name in order ) for obj in objs)

console.log result
for r in result
$("body").append r + "<br/>"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: