“ if(数组中为0)” —甚至合法吗? [英] `if (0 in array)` — is this even legal?

查看:228
本文介绍了“ if(数组中为0)” —甚至合法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在写一本有关JavaScript编码的复杂实验文章。我知道有这样的构造

For now I'm writing a complex of laboratory works about coding in JavaScript. I know that there is a construction like

if ('field' in object) {
    /* Do something with object.field */
}

用于确定变量 field 确实存在于 object 中,即使它等于未定义。

to be used to determine that a variable called field really exists in object even if it equals to undefined.

并且我的Firefox Developer Edition 44能够确定数组中是否存在像这样的字段:

And my Firefox Developer Edition 44 is able to determine if there is a field in an array like this:

if (0 in array) {
    /* Do something with first element */
}

问题:这种方法合法吗?它是生活水平的一部分吗?

The question: is this method legal? Is it a part of a living standard or not?

它是什么? >做

令q为具有以下元素的数组:

What it is supposed to do:
Let q be an array with such elements:

[ 5, <1 empty slot>, undefined, 5 ]

然后:


  • q中的0 2中q ,< q中的code> 3 等于 true ;

  • 1在q 中等于 false

  • 0 in q, 2 in q, 3 in q equals to true;
  • 1 in q equals to false.

推荐答案


if(数组中为0) —这甚至合法吗?

当然。 in 运算符检查对象中是否存在属性(直接或通过原型链)。左操作数是属性的名称,右操作数是要检查的对象。如果左侧操作数不是字符串或符号,则将其强制为字符串。这完全在规范中:

Sure. The in operator checks to see if a property exists in the object (directly, or via the prototype chain). The left-hand operand is the name of the property, the right-hand operand is the object to check. If the left-hand operand isn't a string or Symbol, it's coerced to a string. This is all completely within the specification:


  • in 运算符(向下滚动到 RelationalExpression:ShiftExpression中的RelationalExpression

抽象 ToPropertyKey 操作

抽象的 HasProperty 操作

JavaScript的无类型数组是对象(和不是真正的数组,尽管引擎在引擎优化时会对其进行优化 能够)。数组索引是属性名称(从技术上来说是字符串,尽管引擎会尽可能地对其进行优化)。因此,您可以使用 in 来检查数组是否包含属性,方法是使用带有 in 的数组索引和数组。 (您也可以使用 hasOwnProperty theArray.hasOwnProperty(0),但在现代引擎上使用数组的速度较慢。)

JavaScript's untyped arrays are objects (and 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:

让q为具有以下元素的数组:

Let q be an array with such elements:

[ 5, <1 empty slot>, undefined, 5 ]

然后:


  • 0 ,在q 中为2,在q 中为 3等于 true ;

  • q中的1 等于 false

  • 0 in q, 2 in q, 3 in q equals to true;
  • 1 in q equals to false.

是的,这是完全正确的:

Yes, that's perfectly valid:

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>

这篇关于“ if(数组中为0)” —甚至合法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆