为什么JavaScript的"在"如果测试中存在不包含0阵列0时,运营商的回报是真的吗? [英] Why does javascript's "in" operator return true when testing if 0 exists in an array that doesn't contain 0?

查看:87
本文介绍了为什么JavaScript的"在"如果测试中存在不包含0阵列0时,运营商的回报是真的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么中的在Javascript测试运营商如果0数组存在,即使阵列没有出现含有0时返回true?

Why does the "in" operator in Javascript return true when testing if "0" exists in array, even when the array doesn't appear to contain "0"?

例如,返回true,并且是有道理的:

For example, this returns true, and makes sense:

var x = [1,2];
1 in x; // true

这将返回假的,是有道理的:

This returns false, and makes sense:

var x = [1,2];
3 in x; // false

然而,这返回true,我不明白为什么:

However this returns true, and I don't understand why:

var x = [1,2];
0 in x;


推荐答案

它指的是索引或键,而不是值。 0 1 是数组的有效索引。也有有效的密钥,包括长度toSource。尝试 2 X 。这将是错误的(因为JavaScript的数组是0索引)。

It refers to the index or key, not the value. 0 and 1 are the valid indices for that array. There are also valid keys, including "length" and "toSource". Try 2 in x. That will be false (since JavaScript arrays are 0-indexed).

查看<一个href=\"https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/in_Operator\">MDC文档。

这篇关于为什么JavaScript的&QUOT;在&QUOT;如果测试中存在不包含0阵列0时,运营商的回报是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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