一、instanceof
用来判断右边构造函数的原型对象,是否在左边实例对象的原型链上。一般用来判断引用数据类型的判断。
instanceof 运算符用来测试一个对象在其原型链中是否存在一个构造函数的 prototype 属性,意思就是该变量通过原型链上能否找到构造函数的prototype 属性。(引自判断一个变量是数组还是对象_Yucihent的博客-CSDN博客_判断是数组还是对象)
var arr = ['张三','李四','王五'];
var obj = {
a: '张三',
b: '李四',
c: '王五'
};
console.log(arr instanceof Array); //true
console.log(arr instanceof Object); //true
console.log(obj instanceof Array); //false
console.log(obj instanceof Object); //true
数组也是对象的一种,所以使用instanceof都会返回true
Array.prototype === arr.__proto__
Object.prototype === arr.__proto__.__proto__
注意:instanceof不能判断null和undefined
如果通过 字面量 的方式创建字符串,那么无法通过 instanceof 判断某个变量是否是字符串
let str2 = 'aaaa'
console.log(str2 instanceof String) // false
console.log(str2 instanceof Object) // false
通过 new 方式,是可以使用 instanceof 判断 变量是否是字符串。
let str1 = new String('aaa')
console.log(str1 instanceof String) // true
console.log(str1 instanceof Object) // true
console.log(str1.__proto__ === String.prototype) // true
instanceof判断基本数据类型方法:自定义instanceof,将原有的 instanceof 方法重定义,换成了typeof
class CheckNumberType {
static [Symbol.hasInstance](x) {
return typeof x === 'number'
}
};
console.log(100 instanceof CheckNumberType);
// true
手动实现instanceof: JS实现instanceof_yin_ping1101的博客-CSDN博客
二、constructor
每个对象都有一个constructor属性,它引用了初始化该对象的构造函数,常用于判断未知对象的类型。
var arr = ['张三','李四','王五'];
var obj = {
a: '张三',
b: '李四',
c: '王五'
};
console.log(arr.constructor === Array); //true
console.log(arr.constructor === Object); //false
console.log(obj.constructor === Array); //false
console.log(obj.constructor === Object); //true
注意:
(1)constructor不能判断undefined和null,因为null、undefined没有construstor属性。
(2)JS对象的constructor是不稳定的,这个主要体现在自定义对象上,当开发者重写prototype后,原有的constructor会丢失,constructor会默认为Object
三、Object.prototype.toString.call()
call()改变this指向
toString是Object原型对象上的一个方法,该方法默认返回其调用者的具体类型,更严格的讲,是 toString运行时this指向的对象类型, 返回的类型格式为[object,xxx],xxx是具体的数据类型,基本上所有对象的类型都可以通过这个方法获取到。
console.log(Object.prototype.toString.call("张三"));//[object String]
console.log(Object.prototype.toString.call(123));//[object Number]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call(null));//[object Null]
console.log(Object.prototype.toString.call({name: "张三"}));//[object Object]
console.log(Object.prototype.toString.call(function(){}));//[object Function]
console.log(Object.prototype.toString.call([]));//[object Array]
console.log(Object.prototype.toString.call(new Date));//[object Date]
console.log(Object.prototype.toString.call(/\d/));//[object RegExp]
四、ES6的方法——Array.isArray(value)
如果 value 是一个数组,则返回 true;否则返回 false。
console.log(Array.isArray({})); // false
console.log(Array.isArray([])); // true
五、不能用typeof,typeof用于判断一个变量的类型,适用于基本类型、function,typeof不能判断引用类型(除function)的对象的具体类型,typeof判断数组[]、对象{}、null的结果都是 object。
console.log(typeof a); //'undefined'
console.log(typeof(true)); //'boolean'
console.log(typeof '123'); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof null); //'object'
var arr=["aa","bb"]
console.log(typeof arr);//'object'
var obj = {
a: '张三',
b: '李四',
c: '王五'
};
console.log(typeof obj);//'object'
var obj2 = new String();
console.log(typeof(obj2)); //'object'
console.log(typeof new Date());//'object'
var fn = function(){};
console.log(typeof(fn)); //'function'
console.log(typeof(class c{})); //'function'
null为什么被typeof判断为'object'?
从用法来讲,null经常用于释放一个变量对于对象的引用,创建对象时,对象名也就是变量存储的是一个地址,地址指向一个对象,null常用来打断这个引用,代替变量中的对象地址,让变量不再引用原来的对象,因此null也被认为是一种特殊的空地址,所以从用法上来说,null更像一个没有意义的对象object。
另外,在 JavaScript 第一个版本中,所有值都存储在 32 位的单元中,每个单元包含一个小的 类型标签(1-3 bits) 以及当前要存储值的真实数据。类型标签存储在每个单元的低位中,其中 null的类型标签和Object的类型标签一样都是000,因此被判定为Object
000: object - 当前存储的数据指向一个对象。
1: int - 当前存储的数据是一个 31 位的有符号整数。
010: double - 当前存储的数据指向一个双精度的浮点数。
100: string - 当前存储的数据指向一个字符串。
110: boolean - 当前存储的数据是布尔值。