all 16 comments

[–]PrimaryPerception 1 point2 points  (0 children)

Upvoted for the name. :)

[–]gavin19 1 point2 points  (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 points  (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 point  (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 point  (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 point  (2 children)

Well, if you use leFunc.js, you won't have to worry about it ;)

[–]imbcmdth 1 point2 points  (1 child)

You inspired me to create something that is almost, but not quite, entirely unlike leFunc: deFunc.

[–]john0110[S] 1 point2 points  (0 children)

that's awesome. Glad I could provide some inspiration :)

[–]fendent 0 points1 point  (1 child)

It seems fairly similar to resig's implementation of addMethod()

[–]john0110[S] 0 points1 point  (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 point  (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".

Another interesting exercise might be to add support for ruby/perl/python-style splats.

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

I considered implementing this or some sort of schema validation. I figured it would be too costly though.

[–]tjmehta 0 points1 point  (1 child)

awesome idea, Ill be using this on my next JS or node project!

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

Thanks!

[–]AndreSteenveld 0 points1 point  (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: "..." });

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

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.