instanceof与typeof

typeof

typeof 用于判断简单数据类型

例外 null

typeof null //object

typeof 检测复杂数据类型 结果是object

例外 function

typeof function //function

instanceof

作用

用于检测构造函数的 prototype 属性是否出现在某个实例对象的原型链上,存在返回true,不存在返回false

语法

实例对象 instanceof 构造函数

使用instanceof判断是否通过该构造函数创建

不准确

let arr = []
arr的原型链

arr ==> Array.prototype ==> Object.prototype ==> null

console.log(arr instanceof Array) // true
console.log(arr instanceof Object) // true

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注