My argument sanitization lib has a byproduct which does some typechecking and constructor investigation. I'm unsure if I'm using the fastest / most efficient approach here.
What are your opinions? Total rubbish or does it have a right to exist? ;)
// Return the type of an object aka safe typeof
function typeOf(o) {
return Object.prototype.toString.call(o).match(/(\w+)\]/)[1];
}
// Return the name of a function aka class name
function nameOf(o) {
return typeOf(o) == "Function"
? Function.prototype.toString.call(o).match(/function\s?(\w*)\(/)[1]
: false;
}
// Return the expected arguments of a function
function argumentsOf(o) {
if(typeOf(o) == "Function") {
var args = Function.prototype.toString.call(o).match(/\((.+)\)\s*{/);
if(!!args && !!args[1]) return args[1].replace(/\s+/g, "").split(",");
else return [];
} else return false;
}
"anonymous"
instead offalse
for a function name. Also it will throw an error for[1]
because.match()
can returnnull
– Esailija Nov 9 '12 at 0:41false
. Are there any substantial benefits? – buschtoens Nov 9 '12 at 6:17[1]
should therefore always match. But just to be safe, I'll check that. Thanks. – buschtoens Nov 9 '12 at 6:19false
is not a string, though returning an empty string is viable as well. Getting a boolean from a function namednameOf
is kinda iffy :P – Esailija Nov 9 '12 at 11:42false
is returned when no function was given andnameOf
doesn't know what to do.""
is returned when there is no function name. I think it's safer than"anonymous"
, because some weird users might name their functionanonymous
and then things would blow up. – buschtoens Nov 9 '12 at 21:04