开发者

Distinguish between Node's native functions and anonymous functions

开发者 https://www.devze.com 2023-02-15 06:08 出处:网络
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.

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

精彩评论

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