Created
November 27, 2010 18:24
-
-
Save taitsmp/718140 to your computer and use it in GitHub Desktop.
Javascript function as an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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