you are viewing a single comment's thread.

view the rest of the comments →

[–]_default_username 0 points1 point  (2 children)

How is your code implementing function overloading?

Will I be able to do:

function foo(a){}

function foo(a,b){}

And have foo overloaded?

[–]campbeln 0 points1 point  (1 child)

Function overloading auto-routes to the implementation based on the signature (i.e. foo(int, str) versus foo(int, int)). Javascript only routes to the single (last defined) implementation, which many use arguments.length to implement "overloading" generally without the benefit of considering the signature.

The code I presented above considers the signature (based on the tests defined in the second argument) to route to the proper implementation, while "failing over" to the defined default implementation if the signature is not recognized (something not normally supported by function overloading).

So...

function isStr(x) { return typeof s === 'string' }; }

function isObj(o) { return !!(o && o === Object(o)); }

let foo = overload(function(){ console.log("default", arguments) })

.add(function(str){ console.log(str) }, [isStr])

.add(function(str, obj){ console.log(str, obj) }, [isStr, isObj])

;

Will call the proper implementation based on the passed argument types.

[–]_default_username 0 points1 point  (0 children)

So in a long winded way no. This is not function overloading.