使用instanceof 方法。Array instanceof Array是false Array instanceof Object 是true
时间: 2023-10-26 21:37:04 浏览: 40
。这是因为在Javascript中,Array是Object的子类,因此Array可以被视为一种特殊类型的Object。而instanceof方法用于检查一个对象是否是某个类的实例,它会逐级向上查找这个对象的原型链,并判断是否存在该类的原型对象。因此,当我们使用Array instanceof Object时,由于Array是Object的子类,所以返回值为true;而当我们使用Array instanceof Array时,由于Array并不是Array的子类,而是Array的实例,所以返回值为false。
相关问题
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 的原型链上。
Object instanceof Object 为true而Array instanceof Array 为false原因是啥
这是因为JavaScript中的所有对象都是Object的实例,包括数组(Array)。但是由于数组有自己独特的行为和属性,因此Array的实例并不是其它数组的实例。也就是说,Array的原型链上并没有Array自身,而是Object。因此,Array instanceof Array返回false。
相关推荐
![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)