判断方法
-
typeof
-
instanceof
-
Object.prototype.toString.call() (对象原型链判断方法)
-
constructor(用于引用数据类型)
使用方法
-
typeof
方法: typeof x || typeof(x)
return: 数据类型(字符串)
-
instanceof
方法: x instanceof 类型(Array || Object || …)
return: boolean
-
Object.prototype.toString.call()
方法: Object.prototype.toString.call(x)
return: [object 类型] (字符串)
-
constructor
方法: x.constructor (x.constructor === 类型)返回boolean值
return [Function 类型](字符串)
作用范围
-
typeof
// 判断基本类型,引用类型不合适 let number = 1; //数值 let str = "abc"; //字符串 let boolean = true; let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型 const obj = {name:'man杨杨'}; const arr = [1,2,3,4]; const func = ()=>{} console.log(typeof number); //number console.log(typeof NaN); //number console.log(typeof bigint); //bigint console.log(typeof str); //string console.log(typeof boolean); //boolean console.log(typeof null); //object console.log(typeof undefined); //undefined console.log(typeof obj); //object console.log(typeof arr); //object console.log(typeof func); //function console.log(typeof(number)); //number // 结论: // 优点:基本类型可以做到很好的区分 // 缺点:typeof不能区分 null object arr
-
instanceof
let number = 1; //数值 字面量创建 let number1 = new Number(1); //数值 通过new实例创建 const obj = {name:'man杨杨'}; const arr = [1,2,3,4]; console.log(number instanceof Number); //false 使用字面量创建的基本类型变量 会返回 false console.log(number1 instanceof Number); //true console.log(obj instanceof Object); //true console.log(arr instanceof Array); //true // 总结: // instanceof主要用来判断是Array还是Object,不太适合用来判断基本类型 // instanceof 主要和原型、原型链有关 // instanceof 主要的实现原理就是只要右边变量的 prototype 在左边变量的原型链上即可。
-
Object.prototype.toString.call()
let number = 1; //数值 let str = "abc"; //字符串 let boolean = true; let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型 const obj = {name:'man杨杨'}; const arr = [1,2,3,4]; const func = ()=>{}; console.log(Object.prototype.toString.call(number)); //[object Number] console.log(Object.prototype.toString.call(str)); //[object String] console.log(Object.prototype.toString.call(boolean)); //[object Boolean] console.log(Object.prototype.toString.call(bigint)); //[object BigInt] console.log(Object.prototype.toString.call(obj)); //[object Object] console.log(Object.prototype.toString.call(arr)); //[object Array] console.log(Object.prototype.toString.call(func)); //[object Function] console.log(Object.prototype.toString.call(new Date()));//[object Date] console.log(Object.prototype.toString.call(/a/)); //[object RegExp] let t = Object.prototype.toString.call(obj); //[object Object] console.log(Object.prototype.toString.call(t)); //[object String] /* 总结 可以判断任意数据类型 */
-
constructor
let number = 1; //数值 let str = "abc"; //字符串 let boolean = true; let bigint = 123123n; //bigInt类型,数字后面加n ES6新出的数据类型 const obj = {name:'man杨杨'}; const arr = [1,2,3,4]; const func = ()=>{}; console.log(number.constructor === Number); //true console.log(str.constructor === String); //true console.log(boolean.constructor === Boolean); //true console.log(bigint.constructor === BigInt); //true console.log(obj.constructor === Object); //true console.log(arr.constructor === Array); //true console.log(func.constructor === Function); //true /* 总结 因为 null、undefined 是无效的对象,所以他们没有constructor constructor 判断类型的限制过多且不准确,不推荐使用 */