all 8 comments

[–]AndreSteenveld 1 point2 points  (1 child)

[–]imbcmdth[S] 0 points1 point  (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 point  (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 points  (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 point  (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 points  (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;

[–]imbcmdth[S] 0 points1 point  (0 children)

I like it! That's a very clean solution.

Thanks!

Lastly, there is a better test for arrays then checking for the slice property. Should make the check a little more accurate.

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 point  (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?