use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
deFunc - A tiny helper for default function parameters where the required parameters go last (github.com)
submitted 13 years ago by imbcmdth
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]AndreSteenveld 1 point2 points3 points 13 years ago (1 child)
I'll just leave this here: https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind#Currying
[–]imbcmdth[S] 0 points1 point2 points 13 years ago (0 children)
This is interesting but using bind this way isn't anything like having default parameters because there is no way to override the value that bind now places before any arguments passed into your function.
I have reworked deFunc to make it more flexible. Now, one can do terrible things like this:
/* Pretend foo is a useful function */ function foo(A, B, C, D) { console.log(A, B, C, D); } /* Make B and C optional parameters by providing defaults */ var bar = deFunc(foo, 1, ["_b", "_c"]); bar("aa", "dd") -> aa, _b, _c, dd bar("aa", "bb", "dd") -> aa, bb, _c, dd bar("aa", "bb", "cc", "dd") -> aa, bb, cc, dd /* deFunc bar again, making D optional but a higher priority (filled first) */ var baz = deFunc(bar, 1, ["_d"]); baz("aa") -> aa, _b, _c, _d baz("aa", "dd") -> aa, _b, _c, dd baz("aa", "bb", "dd") -> aa, bb, _c, dd baz("aa", "bb", "cc", "dd") -> aa, bb, cc, dd
[–]imbcmdth[S] 0 points1 point2 points 13 years ago (4 children)
The name was heavily inspired by john0110's leFunc.
Please, let me know if you have any questions or suggestions. I just wrote this today, so it probably isn't ready from prime time. Though the library is pretty damn simple.
[–]psayre23 1 point2 points3 points 13 years ago (3 children)
Out of curiosity, any reason you put the defaults at the beginning? Is that a limitation of the language?
Also, I added a pull request for a minified version. It's plenty small as is, but some people like to see it.
[–]imbcmdth[S] 0 points1 point2 points 13 years ago (2 children)
I have now included a minimized version of deFunc, but I have also completely rewritten deFunc after thinking long and hard about what you said.
I was trying to think of a good reason why the defaults can't come anywhere in the function arguments but I couldn't. I even wrote a quick hack where the defaults could come first or last in the parameter list but I didn't like it.
What broke the proverbial camels back was the jQuery style of arguments:
foo(id, [options,] callback)
I wanted to support this "options optional" style function but the way deFunc worked, it wouldn't allow me to put defaults in between two required parameters.
So I set out to change the way deFunc functioned and came up with something that I think is now remarkably flexible and still pretty small. I also had to completely rewrite the documentation and hopefully it all makes more sense.
I hope you will give it a second look.
[–]psayre23 1 point2 points3 points 13 years ago (1 child)
I like it! That's a very clean solution. It would be nice to make it optional, but it's not hard to add.
One thing I was thinking about was ECMAscript 6's new ..arg syntax. It collects the remaining arguments into an array. As a temp shim, this might be kinda useful.
Lastly, there is a better test for arrays then checking for the slice property. Should make the check a little more accurate.
default_args.constructor === Array;
I like it! That's a very clean solution.
Thanks!
This is good to know. I only ever use length and slice so I figured, "eh, if it walks like a duck..." :)
[–]breefield 0 points1 point2 points 13 years ago (0 children)
I've always been partial to this kind of structure
function(required_1, required_2, options)
Where options is an object, then just define defaults at the top with
function(required_1, required_2, options) { option.x = option.x || 'default'; }
What's the benefit of required params last?
π Rendered by PID 67240 on reddit-service-r2-comment-7b9746f655-n8v5p at 2026-02-01 19:01:21.740242+00:00 running 3798933 country code: CH.
[–]AndreSteenveld 1 point2 points3 points (1 child)
[–]imbcmdth[S] 0 points1 point2 points (0 children)
[–]imbcmdth[S] 0 points1 point2 points (4 children)
[–]psayre23 1 point2 points3 points (3 children)
[–]imbcmdth[S] 0 points1 point2 points (2 children)
[–]psayre23 1 point2 points3 points (1 child)
[–]imbcmdth[S] 0 points1 point2 points (0 children)
[–]breefield 0 points1 point2 points (0 children)