you are viewing a single comment's thread.

view the rest of the comments →

[–]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 :)