JavaScript 中的 instanceof Array 是否优于 isArray?

Is instanceof Array better than isArray in JavaScript?

来自这两个帖子:

有几种方法可以检查一个对象是否为数组

  1. variable instanceof Array

  2. Array.isArray(variable)

有人告诉我第二种方法比第一种好。有谁能说说是什么原因吗?

不。 有些情况下 obj instanceof Array 可能为假,即使 objArray.

You do have to be careful with instanceof in some edge cases, though, particularly if you're writing a library and so have less control / knowledge of the environment in which it will be running. The issue is that if you're working in a multiple-window environment (frames, iframes), you might receive a Date d (for instance) from another window, in which case d instanceof Date will be false — because d's prototype is the Date.prototype in the other window, not the Date.prototype in the window where your code is running. And in most cases you don't care, you just want to know whether it has all the Date stuff on it so you can use it.

来源:Nifty Snippets

这个例子,同样适用于数组对象等等。

而 ECMAScript 标准建议的找到 Object 的 class 的标准方法是使用 Object.prototypeisArray(variable) 中的 toString 方法在内部使用它。