- typeof 判断 不能判断对象类型 typeof [] typeof {} typeof null // 都返回 'object'
- instanceof 判断谁是谁的实例 依据是 __proto__
- constructor 可以找到这个变量是通过谁构造出来的
- Object.prototype.toString.call() 不能细分谁是谁的实例
/**
* 一:typeof
*
* 1.typeof 可以用来判断js的原始数据类型
* 2.typeof 判断引用数据类的的时候 判断不准确
*
* 例如:
* console.log(typeof []); // object
* console.log(typeof function () {}); // function
* console.log(typeof new RegExp(/A/)); // object
*
*/
/**
* 二:Object.prototype.toString.call()
*
* 1.可以判断出来原始数据类型是具体的什么类型
* 2.缺点就是不能判断自定义的构造函数类型
*
* 例如:
* console.log(Object.prototype.toString.call([])); // [object Array]
* console.log(Object.prototype.toString.call(function () {})); // [object Function]
* console.log(Object.prototype.toString.call(new RegExp(/A/))); // [object RegExp]
*
* 判断不出来自定义的构造函数A
* class A {}
* let a = new A();
* console.log(Object.prototype.toString.call(a)); [object Object]
*/
/**
* 三:instanceof
* 1.可以判断是不是某个类型的实例
* 2.不能校验原始数据类型
*
*
* console.log([] instanceof Array); // true // 原理 [].__proto__ === Array.prototype
* console.log([] instanceof Object); // true 原理 [].__proto__.__proto__ === Object.prototype
*
* 原始数据类型判断不出来
* console.log('abc' instanceof String); // false
*/
/**
* 四:constructor
* 判断 A的constructor是不是B构造出来的
*
*/
定义一组变量
let num = 123;
let str = 'abcd';
let bool = true;
let obj = { a: '1' };
let arr = [1, 2, 3];
let fun = function () {};
let und = undefined;
let nul = null;
let date = new Date();
let reg = new RegExp();
let err = new Error();
1.使用typeof检测
// 简单数据类型 如数字、字符串、布尔、undefined、函数可以用typeof检测出来
// 复杂数据类型对象、数组、日期、正则、null 得到的都是'object'
console.log(
typeof num, // number
typeof str, // string
typeof bool, // boolean
typeof obj, // object
typeof arr, // object
typeof nul, // object
typeof date, // object
typeof reg, // object
typeof err, // object
typeof fun, // function
typeof und // undefined
);
2.使用instanceof检测
instanceof是通过__proto__来检测当前变量是否属于某个构造函数的
只有被new出来的才属于, null和undefined没有构造函数
console.log(
num instanceof Number, // false
str instanceof String, // false
bool instanceof Boolean, // false
obj instanceof Object, // true
arr instanceof Array, // true
nul instanceof Object, // false
date instanceof Object, // true
reg instanceof Object, // true
err instanceof Object, // true
fun instanceof Function, // true
und instanceof Object // false
);
3.使用constructor检测
console.log(
num.constructor, // Number
str.constructor, // String
bool.constructor, // Boolean
obj.constructor, // Object
arr.constructor, // Array
date.constructor, // Date
reg.constructor, // RegExp
err.constructor, // Error
fun.constructor // Function
// nul.constructor, // null 没有constructor
// und.constructor // undefined 没有constructor
);
4.Object.prototype.toString.call()
console.log(
Object.prototype.toString.call(num), // [object Number]
Object.prototype.toString.call(str), // [object String]
Object.prototype.toString.call(bool), // [object Boolean]
Object.prototype.toString.call(obj), // [object Object]
Object.prototype.toString.call(arr), // [object Array]
Object.prototype.toString.call(date), // [object Date]
Object.prototype.toString.call(reg), // [object RegExp]
Object.prototype.toString.call(err), // [object Error]
Object.prototype.toString.call(fun), // [object Function]
Object.prototype.toString.call(nul), // [object Null]
Object.prototype.toString.call(und) // [object Undefined]
);