天天看点

typeof和instanceof的区别

1:操作数数量不同

typeof的操作数是 1个

instanceof的操作数是 2个

# 1个操作数
console.log(typeof('strings'));
# 2个操作数
var c = new String('Hellos');
console.log(c instanceof String);      

https://link.juejin.cn?target= 2:返回值不同

typeof返回的是字符串 数据类型

instanceof返回的是布尔值

https://link.juejin.cn?target= 3:操作数类型不同

typeof操作的可以是简单数据类型,函数,或者对象

instanceof操作的 左边必须是引用类型 右边必须是函数

具体代码:见区别1

https://link.juejin.cn?target= 4:typeof能识别引用类型但是不能再细分

typeof和instanceof的区别

5:instanceof 的简单理解

判断A是否是B的实例
A instanceof B      

继续阅读