---
title: "Underscore.js与nodejs相结合"
author: "biyeah"
date: "2011-12-14T11:18:44Z"
tags: []
categories: ["Web前端"]
slug: "underscore-js-nodejs"
draft: false
lastmod: "2011-12-14T11:18:44Z"
id: 1309951
url: "https://biyeah.iteye.com/blog/1309951"
---

今天发现这个Underscore.js，文档说
<http://documentcloud.github.com/underscore/>

> Underscore is a utility-belt library for JavaScript that provides a lot of the functional programming support that you would expect in Prototype.js (or Ruby)。

提供了许多类似ruby风格的函数，这是我所喜欢的。
看几个例子。
安装

> npm install underscore 


在nodes.js中测试

```coffeescript
_ = require 'underscore'
_.each [1, 2, 3], (num) -> console.log(num)#=>1 2 3

_.each
  one : 1
  tow:2
  three:3
  (num,key) ->
    console.log "#{key}:#{num}"

a1 =  _.map [1,2,3,4], (num)-> num * 3
_.each a1, (num) -> console.log(num)#=>3 6 9 12
```


有兴趣的读者可到上面的提到的网址看看。