Aug 3

Prototype JS default function arguments

Category: Development

$H() is just one more reason to use Prototype. Coming from a Ruby background, I’ve become very fond of using hashes. I often use them in handling optional arguments to methods, and setting default values for those arguments. I was creating a function in JS, and realized using such methodology would allow for a much more descriptive function.

function handle_notice(options) {
  var default_options = $H({show_notice: false, show_ignore: false})
  options = default_options.merge($H(options))

The above sets up a hash of default options, then merges or updates the default_options with the options hash passed to the function. This allows me to use any combination of arguments, or pass none to use the default.

1 comment

1 Comment so far

  1. [...] This entry was posted in Blogs. Bookmark the permalink. ← Fish Eye Menu [...]

Leave a comment