The JavaScript blog.


node underscore

Underscore, Array.prototype, and You

Posted on .

What's the most dependent module on npm? Underscore.js! Why do people use it? Presumably because they can't be bothered to learn how JavaScript works.

There are things I like about Underscore. No global meddling, sane internals -- it's a solid piece of work. But I occasionally find myself working with programmers who are probably better than me yet have weaker JavaScript skills, and they reach for Underscore like a drunk reaching for cheap gin.

At this point I stay their hand and point to Annotated ECMAScript, highlighting the superpowers baked into Array.prototype. Granted it lacks some of the things Underscore has, but it often does what you want.

Mozilla's documentation is also good because it shows you how to duplicate the functionality with lengthy code samples, which is educational if you take the time to read it.

If this is new to you or you're a little uncomfortable with Array.prototype, start with forEach then map and some. They're the methods that crop up in my own code a lot.

Another related JavaScript quirk is array type checking. Because JavaScript says typeof [1, 2, 3] is 'object' you might want to pack your bags and give up altogether. I don't blame you. But hiding in that ES5 documentation is a beastie called Array.isArray. You'll find it in util in Node, which is puzzling -- you'll be OK using Array.isArray([1, 2, 3]).

I don't think it's a bad thing to depend on Underscore, but it makes me feel like people don't learn Node or indeed JavaScript well enough before flinging their modules up on npm.


libraries data underscore

backbone.js: JavaScript MVC

Posted on .

backbone.js from DocumentCloud is a small (2KB, packed) open-source library which allows you to apply the MVC
(Model-View-Controller) principle to your JavaScript applications. It helps provide a bit of structure for rich client side apps, by
preventing you from having to tie data directly to the DOM. When models
change, the views representing the data can be notified, causing them to
automatically re-render.

To define a model, you extend Backbone.Model with your own
properties and methods, and the framework provides ways to get and set
the attributes, set up validations, serialize the object, detect if it
has changed, etc. To save a valid model to the persistence layer,
Backbone will delegate to its sync function and make a
relevant RESTful request (using JSON by default) to the server.

Backbone proxies to DocumentCloud's
underscore library for dealing with collections of models.

For views, create a custom view class which extends
Backbone.view, and then instantiate it, passing some
options which can include the model, a collection, the HTML element,
etc... You can define a render function on the view to
setup the view (with whatever flavour of templating or DOM manipulation
you like), and this can be bound to model events.

The documentation is well
written and comprehensive, providing good examples, and there's a
test-suite too. Get the code on