Posts tagged default

Sep 02, 2009

JavaScript optional parameter hash with default values

default values javascript optional parameter hash | comments

I have dealt with optional parameters with default values in JavaScript plenty of times in the past. But for some reason it wasn’t immediately obvious to me how to create a nice options hash like I use so frequently when writing my Ruby code. It really is quite simple.

Here are the beginnings of a little function to handle the changing of a timeframe in a dropdown. The very obvious key is to default your options parameter to an empty hash if it comes through as null/undefined. I’m not sure why that eluded me for a while this afternoon. I’m defaulting the possible options in a couple different ways. I’m checking if boolean options are defined before defaulting them. For string options, I’m simply using an || operator.

function on_timeframe_change(options){
  options                     = options || {};
  var hide_selector_on_custom = typeof(options['hide_selector_on_custom']) == 'undefined' ? 
                                  false : 
                                  options['hide_selector_on_custom'];
  var prefix                  = options['prefix'] || '';
  var grab_focus              = typeof(options['grab_focus']) == 'undefined' ? 
                                  true : 
                                  options['grab_focus'];

  . . .

}

Sorry if you cannot handle ternary operators.


Apr 27, 2007

Change default text editor on OS X

OS X default text editor OS X default text editor | comments

I am a Mac OS X rookie, and it shows. I struggled for about 20 minutes trying to figure out how to change my default text editor for TXT files from TextEdit to TextMate. Finally, it is done, and it was mostly easy.

First, select the file and press cmd-i to go to the Info screen for the file. In the “Open With” section, select your favorite text editor (mine is TextMate).

txtinfo

Click “Change All…” and confirm your intentions.

txtchangeall