Array instanceof Array为false;Object instanceof Object为true
时间: 2024-05-31 10:08:39 浏览: 12
。这是因为Array是Object的子类,所以Array同时也是Object的实例。但是,Array是一种特殊的对象,它具有一些数组特有的属性和方法。因此,Array instanceof Array返回false,表示Array不是自己的实例,而是Object的子类实例。而Object instanceof Object返回true,表示Object是自己的实例。
相关问题
Array instanceof Array为false Object instanceof Object为true
这是因为在 JavaScript 中,Array 是一个特殊的对象类型,它继承自 Object。因此,Array 实例也是 Object 实例的一种,但是 Object 实例并不一定是 Array 实例。
当使用 instanceof 运算符来检查一个对象是否是某个类的实例时,它会检查该对象的原型链中是否存在该类的原型。如果存在,则返回 true,否则返回 false。
在本例中,Array instanceof Array 为 false,是因为 Array.prototype 不在 Array 的原型链上。而Object instanceof Object 为 true,是因为 Object.prototype 在 Object 的原型链上。
Array instanceof Array 为false Object instanceof Object 为true 原理
`instanceof` 运算符用于检查一个对象是否是某个构造函数的实例,它可以用来判断一个对象是否属于某一类。在 JavaScript 中,所有的对象都是 Object 类型的实例,而用 `typeof` 操作符检查一个数组时,会返回 `"object"` 类型,因为数组也是一种对象。
当我们使用 `instanceof` 操作符来判断一个对象是否为数组时,它会检查该对象的原型链中是否有 `Array.prototype`。如果有,那么该对象就被认为是一个数组。但是,如果该对象的原型链中没有 `Array.prototype`,那么它就不是一个数组。
因此,当我们使用 `Array instanceof Array` 时,它返回的是 `false`,因为 `Array` 构造函数并没有继承自 `Array.prototype`。而 `Object` 是所有对象的基础,它的构造函数继承自 `Object.prototype`,所以 `Object instanceof Object` 返回的是 `true`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)