Another interesting technique I spotted in the gRaphaƫl code base is the use of a prefixed + used to cast a variable to a numeric type.
var data = [ undefined, null, 1, 10.2, new Date() ]; for (var i = 0, len = data.length; i < len; i++) console.log('cast %o: %o', data[i], +data[i]);
If you run this code, notice how casting undefined returns NaN which will ripple through your calculations. However, my favorite thing about this technique is how it handles Date objects. The following examples are equivalent.
var now = new Date(); console.log('now.valueOf: %o', now.valueOf()); console.log('+now: %o', +now);
Nice.
Can we get the output for those of us who are lazy and don’t want to run it ourselves?
January 21st, 2010
January 21st, 2010