How to programmatically distinguish an arrow function from a regular function?

后端 未结 2 1452
眼角桃花
眼角桃花 2021-01-01 03:33

There is no obvious difference between an arrow function and a regular function.

({}).toString.call(function () {})
\"[object Function]\"
({}).toString.call(         


        
2条回答
  •  一整个雨季
    2021-01-01 04:21

    Uhm, the requirements are a bit weird, but I made some tests and:

    typeof (() => {}).prototype === "undefined"
    

    Is true, while:

    typeof (function () {}).prototype === "undefined"
    

    Is false, so:

    function isArrow(x)
    {
      return typeof (x.prototype) === "undefined"
    }
    

    Fiddle here: https://jsfiddle.net/87kn67ov/

提交回复
热议问题