What are the disadvantages of using instanceof to detect arrays?

What are the disadvantages of using instanceof to detect arrays?

Generally we say to detect the array, we will use

if (value instanceof Array){
    
     
 // 操作数组
}

Using this method, you may seldom notice any problems.
This is what I saw in the book, so I will share it with everyone. Although this is rarely seen

使用 `instanceof` 的问题是假定只有一个全局执行上下文。如果网页里有多个框架,则可能涉及两
个不同的全局执行上下文,因此就会有两个不同版本的 Array 构造函数。如果要把数组从一个框架传
给另一个框架,则这个数组的构造函数将有别于在第二个框架内本地创建的数组。
为解决这个问题,ECMAScript 提供了 Array.isArray()方法。这个方法的目的就是确定一个值是
否为数组,而不用管它是在哪个全局执行上下文中创建的。来看下面的例子:
if (Array.isArray(value)){
    
     
 // 操作数组
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45416217/article/details/113915689