1、typeof只能识别基础类型和引用类型注意:null、 nan、 document.all 的判断
console.log(typeof null); // objectconsole.log(typeof nan); // numberconsole.log(typeof document.all); // undefined
2、constructorconstuctor 指向创建该实例对象的构造函数注意 null 和 undefined 没有 constructor,以及 constructor 可以被改写
string.prototype.constructor = function fn() { return {};};console.log("云牧".constructor); // [function: fn]
3、instanceof语法:obj instanceof type功能:判断 obj 是不是 type 类的实例,只可用来判断引用数据实现思路: type 的原型对象是否是 obj 的原型链上的某个对象注意:右操作数必须是函数或者 class手写 instanceof:
function myinstanceof(fn, obj) { // 获取该函数显示原型 const prototype = fn.prototype; // 获取obj的隐式原型 let proto = obj.__proto__; // 遍历原型链 while (proto) { // 检测原型是否相等 if (proto === prototype) { return true; } // 如果不等于则继续往深处查找 proto = proto.__proto__; } return false;}
4、isprototypeof是否在实例对象的原型链上基本等同于 instanceofconsole.log(object.isprototypeof({})); // falseconsole.log(object.prototype.isprototypeof({})); // true 期望左操作数是一个原型,{} 原型链能找到 object.prototype
5、object.prototype.tostring利用函数动态 this 的特性function typeof(data) { return object.prototype.tostring.call(data).slice(8, -1);}// 测试console.log(typeof(1)); // numberconsole.log(typeof("1")); // stringconsole.log(typeof(true)); // booleanconsole.log(typeof(null)); // nullconsole.log(typeof(undefined)); // undefinedconsole.log(typeof(symbol(1))); // symbolconsole.log(typeof({})); // objectconsole.log(typeof([])); // arrayconsole.log(typeof(function () {})); // functionconsole.log(typeof(new date())); // dateconsole.log(typeof(new regexp())); // regexp
6、鸭子类型检测检查自身属性的类型或者执行结果的类型通常作为候选方案例子:kindof 与 p-is-promisep-is-promise:
const isobject = value => value !== null && (typeof value === "object" || typeof value === "function");export default function ispromise(value) { return ( value instanceof promise || (isobject(value) && typeof value.then === "function" && typeof value.catch === "function") );}
kindof:
function kindof(obj) { var type; if (obj === undefined) return "undefined"; if (obj === null) return "null"; switch ((type = typeof obj)) { case "object": switch (object.prototype.tostring.call(obj)) { case "[object regexp]": return "regexp"; case "[object date]": return "date"; case "[object array]": return "array"; } default: return type; }}
7、symbol.tostringtag原理:object.prototype.tostring 会读取该值适用场景:需自定义类型注意事项:兼容性class myarray { get [symbol.tostringtag]() { return "myarray"; }}const arr = new myarray();console.log(object.prototype.tostring.call(arr)); // [object myarray]
8、等比较原理:与某个固定值进行比较适用场景:undefined、 window、 document、 null 等underscore.js:
总结方法基础数据类型引用类型注意事项
typeof √ × nan、object、document.all
constructor √ 部分 √ 可以被改写
instanceof × √ 多窗口,右边构造函数或者class
isprototypeof × √ 小心 null 和 undefined
tostring √ √ 小心内置原型
鸭子类型 - √ 不得已兼容
symbol.tostring tag × √ 识别自定义对象
等比较 √ √ 特殊对象
加餐:es6 增强的 nannan 和 number.nan 特点typeof 后是数字
自己不等于自己
delete 不能被删除
isnan如果非数字,隐式转换传入结果如果是 nan,就返回 true,反之返回 falseconsole.log(isnan(nan)); // trueconsole.log(isnan({})); // true
number.isnan判断一个值是否是数字,并且值是否等于 nanconsole.log(number.isnan(nan)); // trueconsole.log(number.isnan({})); // false
其他判断是否 nan 的方法
function isnanval(val) { return object.is(val, nan);}function isnanval(val) { return val !== val;}function isnanval(val) { return typeof val === "number" && isnan(val);}// 综合垫片if (!("isnan" in number)) { number.isnan = function (val) { return typeof val === "number" && isnan(val); };}
indexof 和 includesindexof 不可查找 nan,includes 则可以const arr = [nan];console.log(arr.indexof(nan)); // -1console.log(arr.includes(nan)); // true
【推荐学习:javascript高级教程】
以上就是javascript怎么判断数据类型?8 种方式分享的详细内容。
