Posted By

marcio on 02/05/10


Tagged

javascript function arguments


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

novatvstdios
viskenxp


Default arguments in Javascript functions


 / Published in: JavaScript
 

URL: http://parentnode.org/javascript/default-arguments-in-javascript-functions/

  1. Function.prototype.defaults = function()
  2. {
  3. var _f = this;
  4. var _a = Array(_f.length-arguments.length).concat(
  5. Array.prototype.slice.apply(arguments));
  6. return function()
  7. {
  8. return _f.apply(_f, Array.prototype.slice.apply(arguments).concat(
  9. _a.slice(arguments.length, _a.length)));
  10. }
  11. }
  12.  
  13.  
  14. //Usage
  15. var foo = function(a, b)
  16. {
  17. ...
  18. }.defaults(42, 'default_b');

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: viskenxp on February 29, 2012

Extended the function quickly so it does a full merge of objects: http://snipplr.com/view/63636/javascript-function-default-arguments-full-object-merge/

You need to login to post a comment.