美文网首页
typeof bar==='object'来确定bar是否是一个

typeof bar==='object'来确定bar是否是一个

作者: yoolika | 来源:发表于2019-03-04 14:16 被阅读0次

typeof null 也等于object !
所以可以使用((bar !== null) && (typeof bar ==="object"))来判断 //true

但是如果bar是一个函数 上面的解决方案就为false,如果你希望为true 可以用如下:
((bar !== null) && (typeof bar ==="object") && (typeof bar ==="function"))

但是如果bar是一个数组 上面的解决方案就为true, 例如var bar = [] 如果你希望为false 可以用如下:
((bar !== null) && (typeof bar ==="object") && (Object.prototype.toString(bar)! =="[object Array]"))

还有一个方法对空置、数组、函数返回false 但对于对象为true:
((bar !== null) && (bar.constructor === Object))

或者使用JQuery:
((bar !== null) && (typeof bar ==="object") && (!&.isArray(bar))

相关文章

网友评论

      本文标题:typeof bar==='object'来确定bar是否是一个

      本文链接:https://www.haomeiwen.com/subject/qkqiuqtx.html