简体   繁体   English

Javascript中的名称索引-数组/对象?

[英]Name Indexes in Javascript - Array / Object?

I understand that when index names are used to push values in Javascript, they essentially work like objects. 我了解到,当索引名用于在Javascript中推送值时,它们实际上就像对象一样工作。 But what I don't understand is the following behaviour - 但是我不理解的是以下行为-

person = [];
person[0] = "Someone";
person["test"] = "SomeoneElse"

Inputting person on the console prints ["Someone"] and I could see no information about person.test . 在控制台上输入person打印["Someone"] ,我看不到有关person.test信息。 person.test does print SomeoneElse . person.test可以打印SomeoneElse However, if I go console.log(person) , I get ["Someone", test: "SomeoneElse"] . 但是,如果我进入console.log(person) ,则会得到["Someone", test: "SomeoneElse"] Curious to check if this makes sense, I tried to create a structure like this one - 好奇地检查这是否有意义,我试图创建一个像这样的结构-

var experiment = ["Someone1", test1: "SomeoneElse1"]

and what I get is 我得到的是

Uncaught SyntaxError: Unexpected token

What am I missing? 我想念什么?

Thanks in advance! 提前致谢!

Typing person on the console prints ["Someone"] . 在控制台上键入person打印["Someone"]

Array.prototype.toString formats this output, and it only considers the "array values" of itself without other properties. Array.prototype.toString格式化此输出,并且仅考虑自身的“数组值”,而没有其他属性。

However, if I go console.log(person) , I get ["Someone", test: "SomeoneElse"] . 但是,如果我进入console.log(person) ,则会得到["Someone", test: "SomeoneElse"]

console.log outputs other information about the object, including own properties. console.log输出有关对象的其他信息,包括自己的属性。

Uncaught SyntaxError: Unexpected token 未捕获的SyntaxError:意外令牌

Because that is bogus syntax; 因为那是伪造的语法; the array literal syntax doesn't allow keys, because array values aren't supposed to have keys. 数组文字语法不允许使用键,因为不应将数组值包含键。 Arrays are a numerically indexed list of values . 数组是数字索引的值列表 Merely by the fact that under the hood those lists are implemented using objects (because everything in Javascript is an object of some kind or another) are you able to set "non numeric keys" on the array. 仅凭这样一个事实,即可以使用对象(因为Javascript中的所有内容都是某种对象)来实现这些列表,因此您可以在数组上设置“非数字键”。 That doesn't mean you're using the array correctly though. 但这并不意味着您在正确使用数组。

Also see Are JavaScript Array elements nothing more than Array object properties? 另请参见JavaScript数组元素仅是数组对象属性吗?

This is because an array in JavaScript is also an object itself, and objects can have properties. 这是因为JavaScript中的数组本身也是对象,并且对象可以具有属性。 So it is perfectly valid to have an array with elements, but also have properties set on the array object. 因此,拥有一个包含元素的数组是完全有效的,但在该数组对象上设置属性也是如此。

The second example doesn't work because the [...,...,...] syntax is specifically for instantiating an array and its elements. 第二个示例不起作用,因为[...,...,...]语法专门用于实例化数组及其元素。

typing person in console is like having an alert(person); 在控制台中键入人员就像有一个alert(person); or passing its value to a variable or element, so it is more like you want to get the first set of readable values. 或将其值传递给变量或元素,因此更像是要获取第一组可读值。 That is the reason why it is showing you the values inside, you can try adding person[1] = *something;* , then it will display someone, something 这就是为什么它向您显示内部值的原因,您可以尝试添加person[1] = *something;* ,然后它将显示某人,某物

console.log(person) - it displays all the items inside an object. console.log(person) -它显示对象内的所有项目。 It is more like informing you of what is inside, like a text visualizer in your IDE 这更像是通知您内部内容,就像您的IDE中的文本可视化工具一样

var experiment = ["Someone1", test1: "SomeoneElse1"] - it will absolutely throw an exception there is no such format like this on initializing values, at least it is expecting an array format like var experiment = ["Someone1", "SomeoneElse1"] var experiment = ["Someone1", test1: "SomeoneElse1"] -它将绝对引发异常,在初始化值时没有这样的格式,至少它期望像var ["Someone1", "SomeoneElse1"] = ["Someone1", "SomeoneElse1"]

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

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