JavaScript:测试未定义值的变量;测试对象是否是数组 [英] JavaScript: Testing variables for undefined value; testing whether object is array
问题描述
-
typeof value ===undefined
和值是否有区别=== undefined
?
为什么JavaScript需要 Array.isArray()
in ECMAScript 5?我不能只调用 value instanceof Array
来确定给定变量是否是一个数组?
Why did JavaScript need Array.isArray()
in ECMAScript 5? Can't I just call value instanceof Array
to determine whether an given variable is an array?
推荐答案
//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
。
There are also issues with undefined = true
being valid. But you don't really care about that. No-one is stupid enough to alter undefined
globally these days.
我也知道<$ c的错误$ c> instanceof 被破坏了。我不能告诉你为什么 Array.isArray
更好的原因。
I also know of bugs with instanceof
being broken. I can't give you the exact reason why Array.isArray
is better though.
你会发现批评 JavaScript Garden 中的 instanceof
如果你阅读这篇文章它提到了 instanceof
在单独的框架/窗口/ iframe中是如何工作的。
If you read this Article It mentions how instanceof
does not work across seperate frames / windows / iframes.
因为instanceof检查了code>数组,每个窗口都有自己的 window.Array
。
Because instanceof checks againts Array
and each window has it's own window.Array
.
这篇关于JavaScript:测试未定义值的变量;测试对象是否是数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!