开发者

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 开发者_C百科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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜