stretchr / over.js

Elegant function overloading in JavaScript.
Other
148 stars 10 forks source link

Minify? #3

Open elclanrs opened 10 years ago

elclanrs commented 10 years ago

Nice script, I can see myself using this often but as-is it would break when minified, given that you're serializing the function. A workaround could be the approach that Angular takes, passing an array with the arguments as strings so they don't get mangled after minifying. Maybe something like this? Just thinking out loud:

var f = Over(
 ['msg$string', function(msg) {
   return ...;
 }],
 ['msg$string, num$number', function(msg, num) {
   return ...;
 }]
);
matryer commented 10 years ago

Great point. Can you provide some examples?

elclanrs commented 10 years ago

See my update above.

matryer commented 10 years ago

What about this:

var f = Over({
  "string, number": function(s, n){ /* ... */ },
  "string, string, number": function(s, s2, n){ /* ... */ },
  "string, etc": function(s, etc){ /* ... */ }
});
matryer commented 10 years ago

(We could then support both versions)

elclanrs commented 10 years ago

Yeah, that's much better than my proposed solution, looks clean.

elclanrs commented 10 years ago

I got inspired by your script and gave it a try at building my own way following the solution you proposed. The script is very simple, it doesn't do any serialization or even throw exception, it just fallbacks to the closest signature or to an empty function. Extra arguments are always passed as an array at the end. I uploaded a little gist if you want to take a look. https://gist.github.com/elclanrs/6986187

floatdrop commented 10 years ago

:+1: for passing object to Over. This allows optional arguments be notes more human readable, instead of $_.

var f = Over({
  "string, object?, function": function(name, options, callback){ /* ... */ },
  "string, [object], function": function(s, s2, n){ /* ... */ }
});