简体   繁体   English

javascript运算符“in”

[英]javascript operator “in”

I'm used to python, so 我习惯了python,所以

a = [1,2,3]
1 in a # -> True
b = ["1", "2", "3", "x"]
"x" in b # -> True

Why is it that in JavaScript 为什么在JavaScript中呢?

a = [1,2,3]
1 in a // -> true
b = ["1", "2", "3", "x"]
"x" in b // -> false

and much weirder 而且更奇怪

"1" in b // -> true

in works on KEYS of arrays, not values. in数组的KEYS上工作,而不是值。 1 in a succeeds because there is an element #1 in your array, which is actually the 2 value. 1 in a成功,因为数组中有一个元素#1,实际上是2值。

"1" fails, because there is no 1 PROPERTY or KEY in your array. "1"失败,因为阵列中没有1 PROPERTYKEY

Details here: https://developer.mozilla.org/en/JavaScript/Reference/Operators/in 详情请访问: https//developer.mozilla.org/en/JavaScript/Reference/Operators/in

The thing you have to understand about JavaScript is almost everything is an "Object" that can have properties. 你必须要了解的关于JavaScript的事情几乎所有东西都是一个可以拥有属性的“对象”。 Array's are just a special type of object whose properties are integer indexes and have push, pop, shift, unshift, etc. methods. 数组只是一种特殊类型的对象,其属性是整数索引,并具有push,pop,shift,unshift等方法。 Plus they can be defined with the square bracket shorthand you used: 另外,它们可以使用您使用的方括号速记来定义:

a = [1,2,3];

This creates an Array object with the properties: 这将创建一个具有以下属性的Array对象:

a[0] = 1;
a[1] = 2;
a[2] = 3;

Now as others have said, all the in operator does is check that an object has a property of that name and a[1] == 2 therefore 1 in a == true . 现在正如其他人所说的,所有in运算符都检查对象是否具有该名称的属性,并且a[1] == 2因此1 in a == true On the other hand, 另一方面,

b = ["1", "2", "3", "x"];

created an Array object with the properties: 创建了一个具有以下属性的Array对象:

b[0] = "1";
b[1] = "2";
b[2] = "3";
b[3] = "x";

So b["x"] == undefined therefore "x" in b == false . 所以b["x"] == undefined因此"x" in b == false

The other thing you have to understand is JavaScript uses "duck typing", meaning if it looks like a number, JavaScript treats it like a number. 你需要了解的另一件事是JavaScript使用“duck typing”,这意味着如果它看起来像一个数字,JavaScript会把它当成一个数字。 In this case, b["1"] == 2 therefore "1" in b == true . 在这种情况下, b["1"] == 2因此"1" in b == true I'm not 100% certain whether this is duck typing at work or JavaScript just always treats property names as Strings. 我不是100%肯定这是在工作中打字还是JavaScript只是总是将属性名称视为字符串。

If you wanted to declare a generic object that wouldn't have the Array methods but had the same properties you would write: 如果您想声明一个通用对象,该对象不具有Array方法但具有相同的属性,您可以编写:

var b = {"0": "1", "1": "2", "2": "3", "3": "x"};

Which is shorthand for: 这是简写​​:

var b = {}; // This declares an Object
b[0] = "1";
b[1] = "2";
b[2] = "3";
b[3] = "x";

Because "x" in b is looking for a property name 'x' . 因为"x" in b正在寻找属性名称'x' There is none, they are all numerical (but still string) property names, considering it is an array. 没有,它们都是数字(但仍然是字符串)属性名称,因为它是一个数组。

1 is a valid vanilla numeric array index, x is not; 1是有效的vanilla数值数组索引, x不是; in works with array indexes/object member names, not their values. in与数组索引/对象成员名称,而不是它们的值的作品。

“1”不是数组对象的属性...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM