1、typeof
console.log(typeof 123); // number
console.log(typeof true); // boolean
console.log(typeof 'str'); // string
console.log(typeof []); // object
console.log(typeof {}); // object
console.log(typeof function () { }); // function
console.log(typeof undefined); // undefined
console.log(typeof null); // object
- 其中数组、对象、null 都会被判断为 object,其他判断都正确。
2、 instanceof
instanceof 可以正确判断对象的类型,其内部运行机制是判断在其原型链中能否找到该类型的原型。
console.log(123 instanceof Number); // false
console.log('str' instanceof String); // false
console.log(true instanceof Boolean); // false
console.log([] instanceof Array); // true
console.log({} instanceof Object); // true
console.log(function () { } instanceof Function); // true
- 可以看到,instanceof 只能正确判断引用数据类型,而不能判断基本数据类型。instanceof 运算符可以用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性。
3、 constructor
console.log((123).constructor === Number); // true
console.log(('str').constructor === String); // true
console.log(([]).constructor === Array); // true
console.log(({}).constructor === Object); // true
console.log((function () { }).constructor === Function); // true
constructor 有两个作用,一是判断数据的类型,二是对象实例通过constrcutor 对象访问它的构造函数。
需要注意,如果创建一个对象来改变它的原型,constructor 就不能用来判断数据类型了:
function Fun () { };
Fun.prototype = new Array();
let f = new Fun();
console.log(f.constructor === Fun); // false
console.log(f.constructor === Array); // true
3、Object.prototype.toString.call()
Object.prototype.toString.call() 使用Object 对象的原型方法toString 来判断数据类型:
function objType (obj) {
return Object.prototype.toString.call(obj).slice(8, -1).toLowerCase()
}
console.log(objType(123)); //number
console.log(objType('str')); //string
console.log(objType([])); //array
console.log(objType({})); //object
console.log(objType(null)); //null
console.log(objType(undefined)); //undefined
console.log(objType(function () { })); //function
同样是检测对象 obj 调用 toString 方法,obj.toString()的结果和Object.prototype.toString.call(obj)的结果不一样,这是为什么? 这是因为 toString 是 Object 的原型方法,而Array、function等类型作为 Object 的实例,都重写了 toString 方法。不同的对象类型调用 toString 方法时,根据原型链的知识,调用的是对应的重写之后的 toString 方法(function 类型返回内容为函数体的字符串,Array类型返回元素组成的字符串…),而不会去调用Object 上原型toString 方法(返回对象的具体类型),所以采用obj.toString()不能得到其对象类型,只能将 obj 转换为字符串类型;因此,在想要得到对象的具体类型时,应该调用 Object 原型上的toString方法。