Why use typeof for identifying a function?

后端 未结 6 619
天涯浪人
天涯浪人 2020-12-09 13:03

Are there any significant reasons for using

typeof variable === \'function\'

versus

!!variable.call

for d

6条回答
  •  误落风尘
    2020-12-09 13:41

    Check the assumptions in the post (see Gumbo's comment).

    typeof /regex/ === 'function'
    

    This returns false in Firefox 3.6.13.

    Just for amusement, Firefox 3.6.13:

    typeof /regex/                    // "object"
    /regex/ instanceof RegExp         // true
    /regex/.constructor.name          // RegExp
    (function () {}).constructor.name // Function
    

    IE8:

    typeof /regex/                    // "object"
    /regex/ instanceof RegExp         // true
    /regex/.constructor.name          // undefined
    (function () {}).constructor.name // undefined
    

    Chrome 9:

    typeof /regex/                    // "function"
    /regex/ instanceof RegExp         // true
    /regex/.constructor.name          // "RegExp"
    (function () {}).constructor.name // "Function"
    

提交回复
热议问题