// Don't actually use this. Ever. Thx. | |
(function() { | |
var getClass = {}.toString, slice = [].slice, classNames = { | |
"[object Function]": "Function", | |
"[object Array]": "Array", | |
"[object String]": "String", | |
"[object Boolean]": "Boolean", | |
"[object Number]": "Number", | |
"[object Date]": "Date", | |
"[object RegExp]": "RegExp" | |
}; | |
function getClassOf(value) { | |
var className; | |
if (value === null) { | |
return "Null"; | |
} else if (value == null) { | |
return "Undefined"; | |
} | |
className = getClass.call(value); | |
return classNames.hasOwnProperty(className) ? classNames[className] : "Object"; | |
} | |
Function.overload = function(obj) { | |
return function() { | |
var signature = [], length = arguments.length; | |
while (length--) signature[length] = getClassOf(arguments[length]); | |
signature = signature.join(", "); | |
if (obj[signature]) { | |
return obj[signature].apply(this, arguments); | |
} else { | |
throw new Error('Signature "' + signature + '" not defined.'); | |
} | |
}; | |
}; | |
}()); |
var func = Function.overload({ | |
'Number, String': function(n, s) { | |
return 'The number ' + n + ' was passed, followed by string ' + s; | |
}, | |
'String, Number': function(s, n) { | |
return 'The string ' + s + ' was passed, followed by number ' + n; | |
} | |
}); | |
func(123, 'foo') // "The number 123 was passed, followed by string foo" | |
func('foo', 123) // "The string foo was passed, followed by number 123" | |
func(123, 456) // Error: Signature "number, number" not defined. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
ralphholzmann
commented
Oct 26, 2011
This is cool as hell. |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
cowboy
Oct 26, 2011
Why isPropertyOf.call(classNames, className)
instead of classNames.hasOwnProperty(className)
?
cowboy
commented
Oct 26, 2011
Why |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
cowboy
commented
Oct 26, 2011
Also, this should probably handle |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
kitcambridge
Oct 26, 2011
@cowboy isPropertyOf.call
is an unfortunate habit acquired from using the toString
, slice
, and isPropertyOf
boilerplate in nearly all my projects. Null
and Undefined
as well.
@cowboy |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
cowboy
Oct 26, 2011
You could also do:
if (value === null) {
return "Null";
} else if (value == null) {
return "Undefined";
}
cowboy
commented
Oct 26, 2011
You could also do: if (value === null) {
return "Null";
} else if (value == null) {
return "Undefined";
} |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
kitcambridge
Oct 26, 2011
@cowboy Sure. I thought typeof value == "undefined"
would have been more readable, but value == null
is far shorter.
@cowboy Sure. I thought |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
cowboy
commented
Oct 26, 2011
That's what well-thought-out comments are for! |
This comment has been minimized.
Show comment
Hide comment
This comment has been minimized.
Show comment Hide comment
Indeed! |
This is cool as hell.👏