Daily log - April 23, 2009

by Sean Cribbs

Application accepted. I learned how to use Javascript’s apply method today (properly). For some context, I was using LowPro awesome DOM.Builder functions to create some @option@s programmatically and insert them into a select. The only thing I thought that is exceptionally quirky about the implementation is that the last argument to a “tagFunc” appears to be an array of children. However on closer inspection, the tagFunc actually takes any number of arguments after the attributes as children, but not an array. Unfortunately, since Javascript doesn’t have Ruby’s awesome “splat” operator (*) for arrays, I had to find another way. Mozilla’s Developer Connection really helped out here. At first I was trying this:

var options = [ ]; // build some options
$select({"name": "the_select_box"}, options);

Instead, this is what worked:

var options = [ ]; // build some options
options.unshift({"name": "the_select_box"}); // put the attributes first in the array
$select.apply(this, options); // "this" is not strictly necessary in this case, "null" would work

Yay for higher-order functions! UPDATE It seems Dan has corrected this issue in the LowPro available on GitHub. I’ll be upgrading!

© 2006-present Sean CribbsGithub PagesTufte CSS