javascript怎么判断数据类型?本篇文章给大家分享js 判断数据类型的 8 种方式,有效帮助工作和面试,面试官看了微微一笑。
注意:null、 NaN、 document.all 的判断
console.log(typeof null); // object console.log(typeof NaN); // number console.log(typeof document.all); // undefined
注意 null 和 undefined 没有 constructor,以及 constructor 可以被改写
String.prototype.constructor = function fn() { return {}; }; console.log("云牧".constructor); // [Function: fn]
手写 instanceof:
立即学习“Java免费学习笔记(深入)”;
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; }
console.log(Object.isPrototypeOf({})); // false console.log(Object.prototype.isPrototypeOf({})); // true 期望左操作数是一个原型,{} 原型链能找到 Object.prototype
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
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; } }
class MyArray { get [Symbol.toStringTag]() { return "MyArray"; } } const arr = new MyArray(); console.log(Object.prototype.toString.call(arr)); // [object MyArray]
underscore.js:
方法 | 基础数据类型 | 引用类型 | 注意事项 |
---|---|---|---|
typeof | √ | × | NaN、object、document.all |
constructor | √ 部分 | √ | 可以被改写 |
instanceof | × | √ | 多窗口,右边构造函数或者class |
isPrototypeof | × | √ | 小心 null 和 undefined |
toString | √ | √ | 小心内置原型 |
鸭子类型 | - | √ | 不得已兼容 |
Symbol.toString Tag | × | √ | 识别自定义对象 |
等比较 | √ | √ | 特殊对象 |
typeof 后是数字
自己不等于自己
delete 不能被删除
console.log(isNaN(NaN)); // true console.log(isNaN({})); // true
console.log(Number.isNaN(NaN)); // true console.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); }; }
const arr = [NaN]; console.log(arr.indexOf(NaN)); // -1 console.log(arr.includes(NaN)); // true
【推荐学习:javascript高级教程】
以上就是JavaScript怎么判断数据类型?8 种方式分享的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号