Closures are great for asynchronous logic.
It's mainly about organization of code for me. Having a bunch of local functions to split up what the code is doing is nice.
create: function _create(post, cb) {
// cache the object reference
var that = this;
function handleAll(err, data) {
var rows = data.rows;
var id = rows.reduce(function(memo, item) {
var id = +item.id.split(":")[1];
return id > memo ? id : memo;
}, 0);
id++;
var obj = {
title: post.title,
content: post.content,
id: id,
// refer to the object through the closure
_id: that.prefix + id,
datetime: Date.now(),
type: "post"
}
PostModel.insert(obj, handleInsert);
}
// this function doesn't use the closure at all.
function handleInsert(err, post) {
PostModel.get(post.id, handleGet);
}
// this function references cb and that from the closure
function handleGet(err, post) {
cb(null, that.make(post));
}
PostModel.all(handleAll);
}
Here's another example of a closure
var cachedRead = (function() {
// bind cache variable to the readFile function
var cache = {};
function readFile(name, cb) {
// reference cache
var file = cache[name];
if (file) {
return cb(null, file);
}
fs.readFile(name, function(err, file) {
if (file) cache[name] = file;
cb.apply(this, arguments);
});
}
return readFile;
})();
And another example
create: function _create(uri, cb, sync) {
// close over count
var count = 3;
// next only fires cb if called three times
function next() {
count--;
// close over cb
count === 0 && cb(null);
}
// close over cb and next
function errorHandler(err, func) {
err ? cb(err) : next();
}
// close over cb and next
function swallowFileDoesNotExist(err, func) {
if (err && err.message.indexOf("No such file") === -1) {
return cb(err);
}
next();
}
this.createJavaScript(uri, swallowFileDoesNotExist, sync)
this.createDocumentFragment(uri, errorHandler, sync);
this.createCSS(uri, swallowFileDoesNotExist, sync);
},
The alternative to using closures is currying variable into functions using f.bind(null, curriedVariable)
.
Generally though, asynchronous programming logic uses callbacks and manipulating state in callbacks either relies on currying or closures. personally I prefer closures.
As for uses of prototypical inheritance, it allows OO? Does prototypical inheritance really need to do more then that for it to be considered "useful". It's an inheritance tool, it allows inheritance, that's useful enough.