JS 判断数据类型的 8 种方式

1、typeof

  • 只能识别基础类型和引用类型

注意:nullNaNdocument.all 的判断

console.log(typeof null); // object
console.log(typeof NaN); // number
console.log(typeof document.all); // undefined 

2、constructor

  • constructor 指向创建该实例对象的构造函数

注意 nullundefined 没有 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

  • 是否在实例对象的原型链上
  • 功能基本等同于 instanceof
console.log(Object.isPrototypeOf({})); // false
console.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)); // Number
console.log(typeOf("1")); // String
console.log(typeOf(true)); // Boolean
console.log(typeOf(null)); // Null
console.log(typeOf(undefined)); // Undefined
console.log(typeOf(Symbol(1))); // Symbol
console.log(typeOf({})); // Object
console.log(typeOf([])); // Array
console.log(typeOf(function () {})); // Function
console.log(typeOf(new Date())); // Date
console.log(typeOf(new RegExp())); // RegExp 

6、鸭子类型检测

  • 检查自身属性的类型或者执行结果的类型
  • 通常作为候选方案
  • 例子:kindofp-is-promise

p-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、等比较

  • 原理:与某个固定值进行比较
  • 适用场景:undefinedwindowdocumentnull

underscore.js:

void 0 始终返回 undefinedvoid 后面接任意值都是返回 undefined, 这是为了兼容 IE,因为在 IE 中 undefined 值可以被改写

总结

方法 基础数据类型 引用类型 注意事项
typeof × NaN、object、document.all
constructor √ 部分 可以被改写
instanceof × 多窗口,右边构造函数或者class
isPrototypeof × 小心 null 和 undefined
toString 小心内置原型
鸭子类型 - 不得已兼容
Symbol.toString Tag × 识别自定义对象
等比较 特殊对象

加餐:ES6 增强的 NaN

NaN 和 Number.NaN 特点

1.typeof 判断类型是数字2.自己不等于自己### isNaN

  • 如果非数字,隐式转换传入结果如果是 NaN,就返回 true,反之返回 false
console.log(isNaN(NaN)); // true
console.log(isNaN({})); // true 

Number.isNaN

  • 判断一个值是否是数字,并且值是否等于 NaN
console.log(Number.isNaN(NaN)); // true
console.log(Number.isNaN({})); // false 

综合垫片(如果不支持 Number.isNaN 的话)

if (!("isNaN" in Number)) {Number.isNaN = function (val) {return typeof val === "number" && isNaN(val);};
} 

indexOf 和 includes

  • indexOf 不可查找 NaNincludes 则可以
const arr = [NaN];

console.log(arr.indexOf(NaN)); // -1
console.log(arr.includes(NaN)); // true 

最后

最近找到一个VUE的文档,它将VUE的各个知识点进行了总结,整理成了《Vue 开发必须知道的36个技巧》。内容比较详实,对各个知识点的讲解也十分到位。



有需要的小伙伴,可以点击下方卡片领取,无偿分享

猜你喜欢

转载自blog.csdn.net/Android062005/article/details/129805314