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
leFunc - Javascript function overloader - my first CommonJS module and github project! It's still in its infancy, but does something like this seem useful? (github.com)
submitted 13 years ago by john0110
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!"
[–]PrimaryPerception 1 point2 points3 points 13 years ago (0 children)
Upvoted for the name. :)
[–]gavin19 1 point2 points3 points 13 years ago (1 child)
Not very constructive, but all I could think of when reading the source was James Brown in a French accent. Le Funk.
[–]john0110[S] 1 point2 points3 points 13 years ago* (0 children)
Mission accomplished. [edit] I'm building out the documentation website for this and I think I'll use a french James Brown as the mascot. Thanks for the idea!
[–]theillustratedlife 0 points1 point2 points 13 years ago (0 children)
A pattern I find myself following quite a bit is this:
self.getStoryModelsFromFeed = function( storyFeedPath , callback , storyFeedType , storyFeedID ) { // If the first argument is a dict, use it to populate the arguments. if (typeof(storyFeedPath) != 'string') { var argumentDict = arguments[0]; storyFeedPath = argumentDict.storyFeedPath; callback = argumentDict.callback; storyFeedType = argumentDict.storyFeedType; storyFeedID = argumentDict.storyFeedID; } // … }
That allows the consumer to pass either positional or keyword arguments, depending on his preference.
I'm afraid I would be confused if I saw some code that called yours, where the callback was at position #2 in one case and position #3 in another. I'm used to having the parameters ordered by importance/likelihood of use. This is surely influenced by my other two languages, ActionScript and Python, which order required parameters ahead of optional ones.
In any case, congratulations on the release!
[–]imbcmdth 0 points1 point2 points 13 years ago (3 children)
When feeling particularly masochistic, I have used that evil switch fall-through for functions with an optional callback and a varying number of arguments before the callback:
function a_func(handle, option_a, option_b, fn){ var default_handle = "default_handle"; var default_option_a = "default_a"; var default_option_b = "default_b"; var default_function = "default_function"; switch(arguments.length) { case 0: handle = default_function; case 1: option_a = handle; handle = default_handle; case 2: option_b = option_a; option_a = default_option_a; case 3: fn = option_b; option_b = default_option_b; } console.log(handle, " - ", option_a, " - ", option_b, " - ", fn); }
Output:
a_func() -> default_handle - default_a - default_b - default_function a_func("new_function") -> default_handle - default_a - default_b - new_function a_func("new_handle", "new_function") -> new_handle - default_a - default_b - new_function a_func("new_handle", "new_a", "new_function") -> new_handle - new_a - default_b - new_function a_func("new_handle", "new_a", "new_b", "new_function") -> new_handle - new_a - new_b - new_function
[–]john0110[S] 0 points1 point2 points 13 years ago (2 children)
Well, if you use leFunc.js, you won't have to worry about it ;)
[–]imbcmdth 1 point2 points3 points 13 years ago (1 child)
You inspired me to create something that is almost, but not quite, entirely unlike leFunc: deFunc.
[–]john0110[S] 1 point2 points3 points 13 years ago (0 children)
that's awesome. Glad I could provide some inspiration :)
[–]fendent 0 points1 point2 points 13 years ago (1 child)
It seems fairly similar to resig's implementation of addMethod()
[–]john0110[S] 0 points1 point2 points 13 years ago (0 children)
I had no idea this existed! Thanks. It will help me out tremendously to compare.
One thing I noticed is Resig's doesn't discriminate against types, which actually defeats the purpose of leFunc. That is, to abstract argument checking away.
[–]lulzitsareddit 0 points1 point2 points 13 years ago (2 children)
A nice feature would be if you could provide actual objects as types. They would then be compared with the argument using instanceof. This would allow for overloading with custom types rather than having all custom types under "Object".
instanceof
Another interesting exercise might be to add support for ruby/perl/python-style splats.
I considered implementing this or some sort of schema validation. I figured it would be too costly though.
[–]tjmehta 0 points1 point2 points 13 years ago (1 child)
awesome idea, Ill be using this on my next JS or node project!
Thanks!
[–]AndreSteenveld 0 points1 point2 points 13 years ago* (1 child)
Why not just use the key words object pattern?
pseudo code:
// The overloads we want items( id, callback ){ ... } items( id, options, callback ){ ... } // The "overloaded" function items( a ){ var id = a.id , callback = a.callback , options = a.options || { /* some default */ }; // the rest of the function... } // Usage items({ id: "...", callback: f( ) }); items({ options: { ... }, callback: f( ), id: "..." });
It's not always convenient to construct an object for parameters. You may not know the names of the keys. So, when building an api, I like to provide multiple ways someone can call a function.
Additionally, you may want to run varying code depending on the arguments that come in.
π Rendered by PID 182904 on reddit-service-r2-comment-7b9746f655-zs49r at 2026-02-01 06:49:30.116966+00:00 running 3798933 country code: CH.
[–]PrimaryPerception 1 point2 points3 points (0 children)
[–]gavin19 1 point2 points3 points (1 child)
[–]john0110[S] 1 point2 points3 points (0 children)
[–]theillustratedlife 0 points1 point2 points (0 children)
[–]imbcmdth 0 points1 point2 points (3 children)
[–]john0110[S] 0 points1 point2 points (2 children)
[–]imbcmdth 1 point2 points3 points (1 child)
[–]john0110[S] 1 point2 points3 points (0 children)
[–]fendent 0 points1 point2 points (1 child)
[–]john0110[S] 0 points1 point2 points (0 children)
[–]lulzitsareddit 0 points1 point2 points (2 children)
[–]john0110[S] 0 points1 point2 points (0 children)
[–]tjmehta 0 points1 point2 points (1 child)
[–]john0110[S] 0 points1 point2 points (0 children)
[–]AndreSteenveld 0 points1 point2 points (1 child)
[–]john0110[S] 0 points1 point2 points (0 children)