用于判断右侧构造函数的原型对象是否在左侧实例对象的原型链上。一般用于判断引用数据类型。
判断变量是数组还是对象_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 方法重定义,换成typeof
class CheckNumberType { static [Symbol.hasInstance](x) { return typeof x === 'number' } }; console.log(100 instanceof CheckNumberType); // true
JS实现instanceof_yin_ping1101的博客-CSDN博客
每个对象都有一个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
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]
如果 value 如果是数组,则返回 true;否则返回 false。
console.log(Array.isArray({})); // false console.log(Array.isArray([]); // true
typeof适用于判断变量类型的基本类型,function,
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'
另外,在 JavaScript 第一个版本中,所有值都存储在 32 位的单元中,每个单元包含一个小的 类型标签(1-3 bits) 以及当前要存储值的真实数据。类型标签存储在每个单元的低位中,其中 null的类型标签和Object的类型标签一样都是000,因此被判定为Object
000: object - 当前存储的数据指向一个对象。
1: int - 当前存储的数据是一个 31 位的有符号整数。
010: double - 当前存储的数据指向一个双精度的浮点数。
100: string - 当前存储的数据指向一个字符串。
110: boolean - 当前存储的数据是布尔值。