在JavaScript中,数据类型分为两大类,一种是基础数据类型,另一种则是复杂数据类型,又叫引用数据类型
基础数据类型和引用数据类型的区别,在之前深拷贝的文章中提到过,这里不做详细赘述。
传送门:javaScript中深拷贝和浅拷贝简单梳理
接下来会针对下面几种数据类型,进行校验
// 基本数据类型let str = "abc";let num = 123;let boo = true;let undef = undefined;let testNull = null;let symb = Symbol("user");let bigInt = BigInt(9007199254740999);// 复杂-引用数据类型let arr = [1, 2, 3, 4];let func = function () {};let obj = {};let date1 = new Date();let setObj1 = new Set();let setObj2 = new Set([1, 2, 3]);let mapObj = new Map();typeof操作符,会返回一个字符串,表示未经计算的操作数的类型
/** * typeof 操作符 * * 返回一个字符串,表示未经计算的操作数的类型。 * * */console.log(typeof str); // stringconsole.log(typeof num); // numberconsole.log(typeof boo); // booleanconsole.log(typeof undef); // undefinedconsole.log(typeof testNull); // objectconsole.log(typeof symb); // symbolconsole.log(typeof bigInt); // bigintconsole.log(typeof Object(bigInt)); // objectconsole.log(typeof arr); // objectconsole.log(typeof func); // functionconsole.log(typeof obj); // objectconsole.log(typeof date1); // objectconsole.log(typeof setObj1); // objectconsole.log(typeof setObj2); // objectconsole.log(typeof mapObj); // object使用typeof操作符的时候,我们可以看到一些较为特殊的情况:
instanceof用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。
/** * * instanceof * * 用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上。 * * */console.log(str instanceof String); // falseconsole.log(new String("abc") instanceof String); // trueconsole.log(num instanceof Number); // falseconsole.log(new Number(123) instanceof Number); // trueconsole.log(boo instanceof Boolean); // falseconsole.log(new Boolean(true) instanceof Boolean); // falseconsole.log(undef instanceof undefined);// Uncaught TypeError: Right-hand side of 'instanceof' is not an objectconsole.log(testNull instanceof null);// Uncaught TypeError: Right-hand side of 'instanceof' is not an objectconsole.log(symb instanceof Symbol); // false // Symbol不是构造函数,没有new操作符console.log(bigInt instanceof BigInt); // falseconsole.log(Object(BigInt("22")) instanceof Object); // trueconsole.log(Object(BigInt("22")) instanceof BigInt); // trueconsole.log(arr instanceof Array); // trueconsole.log(arr instanceof Object); // trueconsole.log(func instanceof Function); // trueconsole.log(func instanceof Object); // trueconsole.log(obj instanceof Object); // trueconsole.log(obj instanceof Function); // falseconsole.log(null instanceof Object); // falseconsole.log(date1 instanceof Object); // trueconsole.log(setObj1 instanceof Object); // trueconsole.log(setObj2 instanceof Object); // trueconsole.log(mapObj instanceof Object); // trueconsole.log(setObj1 instanceof Array); // falseconsole.log(setObj2 instanceof Array); // falseconsole.log(mapObj instanceof Array); // false/** * constructor * * 返回创建实例对象的 构造函数的引用。 * * 注意,此属性的值是对函数本身的引用,而不是一个包含函数名称的字符串 * * 构造函数.prototype.constructor() * * */ // 基本数据类型let str = "abc";let num = 123;let boo = true;let undef = undefined;let testNull = null;let symb = Symbol("user");let bigInt = BigInt(9007199254740999);// 复杂-引用数据类型let arr = [1, 2, 3, 4];let func = function () {};let obj = {};let date1 = new Date();function constructorFn() { this.name = "11";}let con1 = new constructorFn();let setObj1 = new Set();let setObj2 = new Set([1, 2, 3]);let mapObj = new Map(); console.log(str.constructor); // Stringconsole.log(num.constructor); // Numberconsole.log(boo.constructor); // Boolean// console.log(testUndefined.constructor); // Cannot read property 'constructor' of undefined// console.log(testNull.constructor); // Cannot read property 'constructor' of nullconsole.log(symb.constructor); // Symbolconsole.log(bigInt.constructor); // BigIntconsole.log(arr.constructor); // Arrayconsole.log(func.constructor); // Functionconsole.log(obj.constructor); // Objectconsole.log(date1.constructor); // Dateconsole.log(constructorFn.constructor); // Functionconsole.log(con1.constructor); // constructorFnconsole.log(setObj1.constructor); // Setconsole.log(setObj2.constructor); // Setconsole.log(mapObj.constructor); // Map/** * * 构造函数校验 * * */console.log(Function.constructor); // Functionconsole.log(Object.constructor); // Functionconsole.log(Array.constructor); // Functionconsole.log(Date.constructor); // Function在使用Object.prototype.toString.call或者Object.prototype.toString.apply检查数据类型之前,我们先了解一下Object.prototype.toString和JavaScript中的构造函数Function的原型方法apply和call:
/** * 返回一个表示该对象的字符串 * * Object.prototype.toString() * * 每个对象都有一个 toString() 方法,当该对象被表示为一个文本值时,或者一个对象以预期的字符串方式引用时自动调用。 * 默认情况下,toString() 方法被每个 Object 对象继承。 * * 如果此方法在自定义对象中未被覆盖,toString() 返回 "[object type]",其中 type 是对象的类型。以下代码说明了这一点: * * */let isObj = { name: "zhangsan" };let isBoolean = true;let isNumber = new Number(123);let isString = "abc";let isFun = new Function();console.log(isObj.toString()); // [object Object]console.log(isBoolean.toString()); // trueconsole.log(isNumber.toString()); // 123console.log(isString.toString()); // abcconsole.log(new Date().toString()); // Thu Apr 28 2022 16:37:19 GMT+0800 (中国标准时间)console.log(isFun.toString()); // function anonymous() {}/** * * call() 使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数,function.call(thisArg, arg1, arg2, ...) * * apply() 使用一个指定的 this 值和单独给出的一个或多个参数来调用一个函数,unc.apply(thisArg, [argsArray]) * * */// call基本使用;function a() { console.log(this);}function b() { console.log(this);}a.call(b); // function b() {}b.call(a); // function a() {}/** * * 使用 toString() 检测对象类型可以通过 toString() 来获取每个对象的类型。 * 为了每个对象都能通过 Object.prototype.toString() 来检测, * 需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式来调用,传递要检查的对象作为第一个参数,称为 thisArg。 * * 那么 Object.prototype.toString 相当于 原生构造函数的实例化对象isNumber,传参数给Object.prototype.toString执行 * 实际上相当于 toString.call(new ***); * * */ let str = "abc";let num = 123;let boo = true;let undef = undefined;let testNull = null;let symb = Symbol("user");let bigInt = BigInt(9007199254740999);// 复杂-引用数据类型let arr = [1, 2, 3, 4];let func = function () {};let obj = {};let date1 = new Date();function testFun() {}let newTest = new testFun();let newFun = new Function();let setObj1 = new Set();let setObj2 = new Set([1, 2, 3]);let mapObj = new Map();console.log(Object.prototype.toString.apply(new String("sss"))); // [object String]console.log(Object.prototype.toString.apply(str)); // [object String]console.log(Object.prototype.toString.call(num)); // [object Number]console.log(Object.prototype.toString.call(boo)); // [object Boolean]console.log(Object.prototype.toString.call(undef)); // [object Undefined]console.log(Object.prototype.toString.call(testNull)); // [object Null]console.log(Object.prototype.toString.call(symb)); // [object Symbol]console.log(Object.prototype.toString.call(Object(bigInt))); // [object BigInt]console.log(Object.prototype.toString.call(bigInt)); // [object BigInt]console.log(Object.prototype.toString.apply(arr)); // [object Array]console.log(Object.prototype.toString.call(func)); // [object Function]console.log(Object.prototype.toString.call(obj)); // [object Object]console.log(Object.prototype.toString.call(date1)); // [object Date]console.log(Object.prototype.toString.call(testFun)); // [object Function]console.log(Object.prototype.toString.call(newTest)); // [object Object]console.log(Object.prototype.toString.call(newFun)); // [object Function]console.log(Object.prototype.toString.call(setObj1)); // [object Set]console.log(Object.prototype.toString.call(setObj2)); // [object Set]console.log(Object.prototype.toString.call(mapObj)); // [object Map]console.log(Array.isArray([1, 2])); // true// 判断空对象function isEmptyObj(obj) { for (name in obj) { console.log(name); return false; // 不是空对象 } return true;}console.log(isEmptyObj({})); // true文章个人博客地址:深度解析javaScript常见数据类型检查校验
欢迎关注公众号:程序猿布欧,不定期更新一些前端入门文章
创作不易,转载请注明出处和作者。