I've use Backbone a lot, so I recently read through the code, to get a better understanding how it's doing it all. If you're relatively new to JavaScript, you might try this yourself; there's a lot you can learn.

These are a few of the good ideas or cool tricks that stuck out to me.

(function () {
    // all your code
}).call(this);

This isn't a new trick, but it's very good practice. By wrapping your entire project in a function that is immediately called, you can be sure you aren't contaminating the global object. And by call-ing it with this, you can be sure that this will be your bridge to the global space from inside the function.

This leads to the next trick . . .

var root = this;

While I'd seen this one before, too, it wasn't until recently that I realized the value of it. JavaScript is often all about callbacks, and each time you enter a function, the value of this changes, potentially. By setting this variable at the top, and then using it everywhere you'd usually use this, your context issues immediately disappear.

var Backbone;
if (typeof exports !== 'undefined') {
    Backbone = exports;
} else {
    Backbone = root.Backbone = {};
}

This little snippet is really handy. At first blush, it can seem tricky to get your library working in both the browser and Node without having two separate builds. This code checks for the existence of an object called exports, which is the object you use to export functionality in a Node module. If it exists, Backbone is pointed to that object. Otherwise, Bsckbone is set to an empty object.

Notice also the way both Backbone and root.Backbone are being set to the same object: since JavaScript variable assignment is an expression that returns the assigned value, you can set multiple variables to the same value in one line.

Note that this is also possible because JavaScript objects are reference values, so the object set to root.Backbone is the same object in memory that is set to Backbone.

if (key == null || typeof key === 'object') {
    attrs = key;
    options = val;
} else {
    (attrs = {})[key] = val;
}

I thought this one was clever. As I just mentioned, variable assignment is an expression. In the else statement, they're using that fact to both set attrs to an empty object and give it a key in one line.

You might think that this is unnecessarily complex, and that attrs = { key: value } would be simpler. But remember that they need to use the square brackets syntax to set this property because actual key name is not "key", but the value of key.

destroy: function(options) {
    var success = options.success;

    options.success = function(resp) {
        if (success) success(model, resp, options);
    };
}

(I've taken the liberty of removing lines unrelated to my point from this function.)

This is a good pattern to use in your libraries: in code you expect to be used by other developers. You know that many Backbone functions take an options object. Many of these public functions, especially Ajax stuff, will call other functions that take options objects of their own. There are several instances of this in the code; this one is for a success function. The pattern here is that the "outer" function takes an options object, and then calls the user's options inside its own options object. Essentially, we're giving the user a hook into a lower level action even as we abstract that away.

_.extend(Collection.prototype, Events, {
    // ...
});

This comes after creating a constructor function. It's more typing to put each property and method on Collection.prototype; using Underscore's extend method cuts characters, and also makes it easier to mix in modules, essentially doing multiple inheritance.

var extend = function(protoProps, staticProps) {
    var parent = this;
    // ...
};

Model.extend = Collection.extend = Router.extend = View.extend = History.extend = extend;

I was curious about how Backbone implemented the extend method that you use to create your own "classes." Turns out, there's this extend function. The details of the function are what you would expect if you've played with JavaScript inheritance before. What interested me was the use of this inside the function: it's treated as the parent class. If the function is used on its own, this will equal the global object, and would probably make a mess. However, once the function is made a property of one of the classes, it does exactly what it needs to.