我所知道的JavaScript中判断数据类型

相信一提到怎么判断js的数据类型,你们都会想到的是typeof、instanceof,那么为何有了typeof的存在还要有instanceof?git

typeof?

根据MDN:typeof操做符返回一个字符串,表示未经计算的操做数的类型。github

eg:面试

typeof 1; // 'number'
typeof NaN; // 'number'
typeof 'zenquan'; // 'string'
typeof true; // 'boolean'
typeof null; // 'object'
typeof undefined; // 'undefined'
typeof Symbol(); // 'symbol'
typeof console.log // "function"
复制代码

typeof出现的问题1——typeof null === 'object'数组

而后你会发现,typeof null; // 'object'。null但是原始数据类型的啊,怎么就是'object'了呢??(不解.jpg)原来这个已是历史问题了,在 JS 的最第一版本中使用的是 32 位系统,为了性能考虑使用低位存储变量的类型信息,000 开头表明是对象,然而 null 表示为全零,因此将它错误的判断为 object 。虽然如今的内部类型判断代码已经改变了,可是对于这个 Bug 倒是一直流传下来。函数

typeof出现的问题2——typeof 引用类型 || Math === ‘object’性能

这样根本不知道是数组仍是对象测试

typeof [] // "object"
typeof {} // "object"
复制代码

typeof出现的问题3——typeof 基本包装类型 || Array ||Date === ‘funtion’ui

这样也不能知道是Number仍是Boolean仍是Stringspa

typeof Number // "function"
typeof Boolean // "function"
typeof String // "function"
复制代码

instanceof?

由于typeof有了以上的问题,因此才有了instanceof。prototype

根据MDN:instanceof运算符用于测试构造函数的prototype属性是否出如今对象的原型链中的任何位置

也就是 p instaceof person === true,则p是person的实例化对象,用于包装对象或者是引用类型对象的判断。

var str1 = 'zenquan';
console.log(str1 instanceof String); // false
var str2 = new String('jomsou');
console.log(str2 instanceof String); // true
复制代码

可能会出现的面试题: 如何判断一个数组?

方法1: instanceof

arr instanceof Array
复制代码

方法2: Array.isArray()

Array.isArray([])
复制代码

结合typeof和instanceof实现判断是否为原始类型

class PrimitiveString {
    static [Symbol.hasInstance](x) {
        return typeof(x) == 'string';
    }
}
console.log('hello world' instanceof PrimitiveString);
复制代码

你可能不知道 Symbol.hasInstance 是什么东西,其实就是一个能让咱们自定义 instanceof 行为的东西,以上代码等同于 typeof 'hello world' === 'string',因此结果天然是 true 了。这其实也侧面反映了一个问题, instanceof 也不是百分之百可信的。

判断数据类型的方法

  1. Object.prototype.toString.call(e).slice(8, -1)
function type(e) {
    return Object.prototype.toString.call(e).slice(8, -1);
}
console.log(type(null))
console.log(type(1))
console.log(type('zenquan'))
console.log(type(undefined))
console.log(type(Symbol()))
console.log(type(true))
console.log(type(console.log))
复制代码
  1. type: JS类型检测库,弥补typeof的问题,原生兼容IE6
  2. 经过一些系统自带的API函数来判断,eg: Array.isArray()

因此,typeof和instanceof结合起来就能够数据类型的判断。

相关文章
相关标签/搜索