-
Paulo Cesar
Hey there, I'm Wynn!
I'm a front-end designer & developer, CSS & JavaScript framework fanboy Razorback living in Texas.
Featured
-
Recently
Archives
Links
The Changelog – Open Source moves fast. Keep up.- cartagen - Client side framework for creating vector maps in HTML5
- NoRM - Bringing MongoDB to .NET, LINQ, and Mono
- Episode 0.1.6 - Ajax.org frameworks with Ruben Daniels and Rik Arends
- Git clone some nostalgia - NCSA Mosiac
- Ghost means never having to touch /etc/hosts again
- Prism - command line and Ruby library parser for Microformats
- Pull! Skeet - Twitter Extension for Google Chrome
- QR - Easy Redis queues with Python
- Faye - dirt simple pub/sub for Rack and Node.js
- FOWA 2010: David Recordon on Facebook Open Source Projects






Rubyists: Meet Underscore.js, your new favorite Javascript library
One of my favorite aspects of developing in Rails is the console. The ability to load and interact with my Ruby objects without using the browser is powerful.
Thanks to Firebug for Firefox and Web Inspector for Webkit, I also have a console for my client-side JavaScript. I wish jQuery were as powerful when traversing raw JavaScript objects as it is traversing the DOM. Well it seems I have two wishes left for the first has been granted in Underscore.js.
Underscore.js: 2kb of pure JavaScript horsepower!
Underscore.js is a new JavaScript framework from DocumentCloud that brings a powerful set of utility functions to JavaScript with a disctinct Ruby flavor and without monkey-patching the native JavaScript types by extending via
prototype. Just as Prototype.js and jQuery employ the$function, Underscore adds all of this goodness via the_function.So, what’s in the box? Let’s just ask
_himself, Ruby style:Nifty, huh? That should look very familiar. Let’s look at
mapas an example.>>> _.map([1, 2, 3], function(num){ return num*num }); => [1, 4, 9]Pretty straightforward. But we can also call Underscore OOP style:
>>> _([1, 2, 3]).map(function(num){ return num*num }); => [1, 4, 9]What’s the advantage you ask? Chaining!
>>> _([1, 2, 3]).chain().map(function(num){ return num*num }).size().value(); => 3Whoa! Why the
at the end?getvalue()UPDATE: Jeremy tells me that in the 0.4.2 release
get()has been renamed tovalue().When chaining, Underscore returns a wrapped set, a special container object (like jQuery does for wrapped sets of elements returned via a selector). To get the last value in the chain, simply call
value.What else is in the box?
Many of the same Ruby Enumerable and Array functions you’ve come to know and love are implemented including:
each,map,detect,select,reject,all,any,include,max,min,sortBy,toArray,size,first,last,compact,flatten,uniq, andzipplus some new ones you’ll love.Underscore also includes also some really useful utility functions.
bind
var func = function(greeting){ return greeting + ', ' + this.name }; func = _.bind(func, {name : 'Adrian'}, 'Yo'); func(); => "Yo, Adrian"template
Here’s an example from the docs:
var list = "<% _.each(people, function(name) { %> <li><%= name %></li> <% }); %>"; _.template(list, {people : ['moe', 'curly', 'larry']}); => " <li>moe</li> <li>curly</li> <li>larry</li> "Download and enjoy
Underscore.js is available in both production and development flavors. Go grab it and have some fun!
Related posts: