Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 the name of a function aka class name
function nameOf(o) {
    return typeOf(o) == "Function"
        : false;

// Return the expected arguments of a function
function argumentsOf(o) {
    if(typeOf(o) == "Function") {
        var args =\((.+)\)\s*{/);
        if(!!args && !!args[1]) return args[1].replace(/\s+/g, "").split(",");
        else return [];
    } else return false;
share|improve this question
You could return the string "anonymous" instead of false for a function name. Also it will throw an error for [1] because .match() can return null – Esailija Nov 9 '12 at 0:41
I prefer false. Are there any substantial benefits? – buschtoens Nov 9 '12 at 6:17
Function should always match the pattern and [1] should therefore always match. But just to be safe, I'll check that. Thanks. – buschtoens Nov 9 '12 at 6:19
false is not a string, though returning an empty string is viable as well. Getting a boolean from a function named nameOf is kinda iffy :P – Esailija Nov 9 '12 at 11:42
I think we got something twisted here: falseis returned when no function was given and nameOf 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 function anonymous and then things would blow up. – buschtoens Nov 9 '12 at 21:04

Fun question,

I believe the only reliable way of getting the name and parameters of a function is to indeed parse the toString. However, you can figure out whether the parameter is a function without a regex. I would propose from here :

function isType(object, type){
     return object != null && type ? === : object === type;

// Return the name of a function aka class name
function nameOf(o) {
    return isType( o , Function )
        : false;

There are still a few things that bother me here, why is the parameter name o, which is the Spartan convention for Object whereas we expect a function ? Also, why name the function nameOf, which creates an expectation of being to name anything whereas it really only can give the name for a function, maybe it ought to be called nameOfFunction or functionName(). Finally, as per the comments, making a function with name return false is making the name lie, just return ''. Something like this then:

// Return the name of a function aka class name
function functionName(f) {
    return isType( f , Function )
        : '';

Finally, for argumentsOf, I sincerely dislike dropping the newline in if statements, removing curly braces is fine, but no newlines makes it too hard to read.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.