Underscore.js, why does `isFunction` use `|| false`?

前端 未结 1 1999
太阳男子
太阳男子 2021-01-18 02:16

The optional override for isFunction(object) in Underscore.js (repo link to definition), reads as follows:

// Optimize `isFunction` if appropria         


        
相关标签:
1条回答
  • 2021-01-18 02:58

    See the issues covered in the comments, #1621, #1929 and #2236.

    Shortly put, some platforms have a bug where typeof isn't a string unless you store it in a variable.
    The || false fixes the issue without introducing an extra variable.

    Taken directly from #1621:

    In IE8, with a variable everything works as expected:

    var t = typeof obj
    t === 'function' // false
    t === 'object' // true
    

    but without one, things are different:

    (typeof obj) === 'function' // true, but SHOULD be false
    (typeof obj) === 'object' // true
    

    The extra check outlined above fixes the bug.

    0 讨论(0)
提交回复
热议问题