JavaScript ES6: Test for arrow function, built-in function, regular function?

后端 未结 10 700
遇见更好的自我
遇见更好的自我 2020-11-27 06:16

Is there an elegant way to tell Harmony\'s slim arrow functions apart from regular functions and built-in functions?

The Harmony wiki states that:

10条回答
  •  [愿得一人]
    2020-11-27 06:29

    Arrow function of JavaScript does not have prototype property.

    function isArrowFunc(fn){
        return typeof(fn)=="function" && fn.prototype === undefined && !(/\{\s*\[native code\]\s*\}/).test(fn.toString());
    }
    
    isArrowFunc(()=>{}) // true
    isArrowFunc(function(){}) // false
    isArrowFunc(123) // false
    

提交回复
热议问题