JS 中的类型判断

105 阅读1分钟

typeof (适合区分基本类型)

typeof null === 'object';
typeof NaN === 'number';
typeof '' === 'string';
typeof false === 'boolean';
typeof Symbol() === 'symbol';
typeof undefined === 'undefined';
typeof function() {} === 'function';
对于复杂类型:数组,对象,正则,Date都会返回 object

instanceof (判断对象的类型,但无法判断基本类型数据)

其实就是递归去遍历一个对象的 __proto__ 和 constructor.prototype 比较

Object.prototype.toString.call(obj)

万能:'[object Array]'