Posted By

1man on 02/27/08


option jquery hash argument

Versions (?)

Who likes this?

1 person have marked this snippet as a favorite


Option Hash Using jQuery

 / Published in: JavaScript

If you have a complex function you may need to pass it lots of arguments. Instead of having to remember how many you need to pass, and passing 'null' for un needed arguments, pass the function an object.

Now you can set the defaults inside the function, which will be superseeded if they are passed as an argument.

  1. var complex = function(valA, options){
  2. /**
  3. * Set the default values in the object, then extend it to include the
  4. * values that we passed to it.
  5. */
  6. var settings = $.extend({
  7. option1: null,
  8. option2: null,
  9. option3: null,
  10. option4: null
  11. },options||{});//If no options, pass an empty object
  12. console.warn(valA);
  13. console.log(settings.option1);
  14. console.log(settings.option2);
  15. console.log(settings.option3);
  16. console.log(settings.option4);
  17. };
  18. complex('Value A', {option1: 'this is option 1'});

Report this snippet  

You need to login to post a comment.