4
  1. typeof value === "undefined"和有区别value === undefined吗?

  2. 为什么Array.isArray()在 ECMAScript 5 中需要 JavaScript?我不能打电话value instanceof Array来确定给定的变量是否是一个数组吗?

4

1 回答 1

7
//var value; There is no var declaration. The variable was never declared

// check againts undeclared variables
typeof value === "undefined"; // works

// check againts declared variables with no value
value === undefined; // ReferenceError: value is not defined

有效性也存在问题undefined = true。但你并不真正关心这一点。这些天没有人愚蠢到在undefined全球范围内改变。

我也知道instanceof被破坏的错误。我不能给你确切的原因为什么Array.isArray更好。

你会instanceofJavaScript Garden中找到批评

如果您阅读这篇文章,它会提到如何instanceof跨单独的框架/窗口/iframe 工作。

因为 instanceof 会进行检查Array,并且每个窗口都有自己的window.Array.

于 2011-05-15T02:41:13.330 回答