对于typeof 值为object,仍想进一步知道对象的类型时,可以使用:
- Object.prototype.toString.call()
<script type="text/javascript">
console.log(Object.prototype.toString.call(123)) //"[object Number]"
console.log(Object.prototype.toString.call('123')) //"[object String]"
console.log(Object.prototype.toString.call(undefined)) //"[object Undefined]"
console.log(Object.prototype.toString.call(true)) //"[object Boolean]"
console.log(Object.prototype.toString.call(null)) //"[object Null]"
console.log(Object.prototype.toString.call({})) //"[object Object]"
console.log(Object.prototype.toString.call([])) //"[object Array]"
console.log(Object.prototype.toString.call(function(){})) //"[object Function]"
</script>
- 当 Array.isArray( ) 方法 不存在的时候, 判断是否是数组就用Object.prototype.toString.call()