Matt
Matt

Reputation: 22921

Distinguish between Node's native functions and anonymous functions

I'm looking for a way to traverse through an object and perform an action if I have an anonymous function and not perform the action if I have a native function.

This is best explained with an example:

User = {
   first : String,
   last : String,
   email : function(email) {
     // ....
   }
}

User.forEach(function(prop) {
   if(! native ) {
      // Do something
   }
});

Gut reaction is of course not, but console.log will output [Function: String] for the String native function, and [Function] for anonymous functions.

Thanks! Matt

Upvotes: 1

Views: 73

Answers (1)

generalhenry
generalhenry

Reputation: 17319

You can use toString() to look at the text of the function then look for functions without names:

/^function\s*?\(/.test(prop.toString())

Upvotes: 1

Related Questions