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))
网友评论