Skip to content

Instantly share code, notes, and snippets.

@taitsmp
Created November 27, 2010 18:24
Show Gist options
  • Save taitsmp/718140 to your computer and use it in GitHub Desktop.
Save taitsmp/718140 to your computer and use it in GitHub Desktop.
Javascript function as an object
//From Pragmatic Guide to Javascript
function repeat(options) {
options = options || {};
for (var opt in (repeat.defaultOptions || {})) {
if (!(opt in options)) {
options[opt] = repeat.defaultOptions[opt];
}
}
for (var index = 0; index < options.times; ++index) {
alert(options.rant);
}
}
//FUNCTION USED AS AN OBJECT TO STORE PROPERTIES.
repeat.defaultOptions = { times: 2, rant: 'IE6 must die!' };
repeat(); // 2 IE6 alert boxes
repeat({ times: 3 }); // 3 IE6 alert boxes
repeat({ times: 2, rant: 'Flash must die!' }); // 2 Flash alert boxes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment