【发布时间】:2017-02-09 16:19:48
【问题描述】:
在this question 中我没有看到使用构造函数的建议。
所以不是typeof callback == "function"
我会使用callback && (callback.constructor==Function)。
在我看来,在运行时性能和编码安全性方面,与内存指针进行比较总是优于与字符串进行比较。
为什么不使用构造函数检测所有类型而忘记丑陋的typeof?
它适用于所有原始类型、函数和数组:
undefined === undefined
null === null
[1,2,3].constructor == Array
(1).constructor == Number
(true).constructor == Boolean
(()=>null).constructor == Function
'abc'.constructor == String
(new Date()).constructor == Date
else it's an object, where instanceof helps to detect it's parents if needed.
如果可以依赖string interning,那么运行时性能优势就会消失。但安全编码优势仍然存在。
【问题讨论】:
-
比较
function和function?
标签: javascript types constructor detection typeof