`if (0 in array)`——这合法吗?
`if (0 in array)` — is this even legal?
目前,我正在编写有关 JavaScript 编码的一系列实验室工作。我知道有一个像
这样的结构
if ('field' in object) {
/* Do something with object.field */
}
用于确定名为field
的变量确实存在于object
中,即使它等于未定义。
而我的 Firefox Developer Edition 44 能够确定数组中是否有这样的字段:
if (0 in array) {
/* Do something with first element */
}
问题:这种方法合法吗?是不是生活水平的一部分?
应该做什么:
令 q 为包含以下元素的数组:
[ 5, <1 empty slot>, undefined, 5 ]
然后:
0 in q
, 2 in q
, 3 in q
等于 true
;
1 in q
等于 false
.
if (0 in array)
— is this even legal?
当然可以。 in
运算符检查对象中是否存在 属性(直接或通过原型链)。左边的操作数是 属性 的名称,右边的操作数是要检查的对象。如果左侧操作数不是字符串或 Symbol
,它会被强制转换为字符串。这完全在 the specification:
之内
The in
operator(向下滚动到 RelationalExpression:ShiftExpression 中的 RelationalExpression)
JavaScript 的无类型数组是对象(和 not really arrays, although engines optimize that when they can). Array indexes are property names (and technically strings, though engines optimize that when they can). So you can use in
to check if an array contains a property by using an "array index" with in
and the array. (You can also use hasOwnProperty
: theArray.hasOwnProperty(0)
, but it's slower with arrays on modern engines。)
What it is supposed to do:
Let q be an array with such elements:
[ 5, <1 empty slot>, undefined, 5 ]
Then:
0 in q
, 2 in q
, 3 in q
equals to true
;
1 in q
equals to false
.
是的,完全正确:
var q = [];
q[0] = 5;
q[2] = undefined;
q[3] = 5;
for (var i = 0; i < q.length; ++i) {
snippet.log(i + " in q? " + (i in q));
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
目前,我正在编写有关 JavaScript 编码的一系列实验室工作。我知道有一个像
这样的结构if ('field' in object) {
/* Do something with object.field */
}
用于确定名为field
的变量确实存在于object
中,即使它等于未定义。
而我的 Firefox Developer Edition 44 能够确定数组中是否有这样的字段:
if (0 in array) {
/* Do something with first element */
}
问题:这种方法合法吗?是不是生活水平的一部分?
应该做什么:
令 q 为包含以下元素的数组:
[ 5, <1 empty slot>, undefined, 5 ]
然后:
0 in q
,2 in q
,3 in q
等于true
;1 in q
等于false
.
if (0 in array)
— is this even legal?
当然可以。 in
运算符检查对象中是否存在 属性(直接或通过原型链)。左边的操作数是 属性 的名称,右边的操作数是要检查的对象。如果左侧操作数不是字符串或 Symbol
,它会被强制转换为字符串。这完全在 the specification:
The
in
operator(向下滚动到 RelationalExpression:ShiftExpression 中的 RelationalExpression)
JavaScript 的无类型数组是对象(和 not really arrays, although engines optimize that when they can). Array indexes are property names (and technically strings, though engines optimize that when they can). So you can use in
to check if an array contains a property by using an "array index" with in
and the array. (You can also use hasOwnProperty
: theArray.hasOwnProperty(0)
, but it's slower with arrays on modern engines。)
What it is supposed to do:
Let q be an array with such elements:
[ 5, <1 empty slot>, undefined, 5 ]
Then:
0 in q
,2 in q
,3 in q
equals totrue
;1 in q
equals tofalse
.
是的,完全正确:
var q = [];
q[0] = 5;
q[2] = undefined;
q[3] = 5;
for (var i = 0; i < q.length; ++i) {
snippet.log(i + " in q? " + (i in q));
}
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="//tjcrowder.github.io/simple-snippets-console/snippet.js"></script>