Function.length


January 17, 2013by @prust; javascript, backbone.js

Little did I know, but the Function prototype has a length property. This is how Backbone knows whether your comparator function should be treated like a _.sortBy() function (that takes a single argument) or an Array.sort() function (that takes two arguments). Perhaps this is also how mocha knows whether your tests take a done parameter. And here I had assumed they were using Function.toString() and parsing out the parameters, as Prototype's Function.argumentNames() does.

One repercussion of this is that if you are passing a single-argument function as a comparator, you have to be careful to not .bind() or _.wrap() it, or Backbone will assume it is a two-argument function.

comments powered by Disqus