简体   繁体   English

什么样的JS对象是[foo:'bar']

[英]What kind of JS object is [ foo: 'bar']

If I create a blank array and assign a value: 如果我创建一个空白数组并分配一个值:

 let a = [] a['foo'] = 'bar' console.log(a) // [ foo: 'bar'] - not in SO console though console.log(a[0]) // undefined console.log(a.length) // 0 console.log(a instanceof Array) // true for (let i of a) {} // Doesn't loop but doesn't throw errors like {} for (let o in a) { console.log(o) // foo } 

What is this? 这是什么? An array?, of length 0? 长度为0的数组? what kind of JS thing is this? 这是什么样的JS东西? also it will print to the node console but not recognized by the SO snippet console (run it and look at the web inspector console at same time) 也会打印到节点控制台,但SO片段控制台无法识别(运行它并同时查看Web检查器控制台)

Note that I don't need answers saying to initialize with {} . 请注意,我不需要用{}初始化的答案。 Thats not the point of the question. 那不是问题的重点。

Everything in javascript is an object so is the Array. javascript中的所有内容都是对象,数组也是。 Array is basically a zero-based keyed object that is iterable so when adding values with string keys into Array you're simply adding keys to that object. Array基本上是一个从零开始的键对象,它是可迭代的,因此,在将带有字符串键的值添加到Array时,您只是在向该对象添加键。

In fact you can create your own object that is iterable just like Array and use it with for...of loops. 实际上,您可以创建自己的可迭代对象,就像Array一样,并将其与for...of循环一起使用。 Check this out: https://jsfiddle.net/gumh6bsy/ 检查一下: https : //jsfiddle.net/gumh6bsy/

If you run let a = [ foo: 'two', bar: 'one' ] you get an error. 如果运行let a = [foo:'two',bar:'one'],则会出现错误。 So how does the loop in my code get that and print that to the console. 那么我的代码中的循环如何获取该结果并将其打印到控制台。 Once the loop creates it it seems perfectly manageable. 循环创建后,它似乎可以很好地管理。 I thought at first it just transformed it into an object but it is an Array instance. 我以为起初它只是将其转换为对象,但它是一个Array实例。 – cyberwombat –网​​络袋熊

let a = [ foo: 'two', bar: 'one' ] spits an error because square brackets are the syntax for initializing arrays and thus gets validated by javascript engine and does not allow definition of keys in the initialization. let a = [ foo: 'two', bar: 'one' ]引发错误,因为方括号是用于初始化数组的语法,因此被JavaScript引擎验证,并且不允许在初始化中定义键。 Meanwhile key assignment syntax ( a['foo'] = 'two' ) is not in anyway different for both arrays and objects. 同时,键分配语法( a['foo'] = 'two' )对于数组和对象都没有任何不同。 In fact it is just a syntax for assigning properties to an object. 实际上,这只是将属性分配给对象的语法。 The main takeaway here is that in javascript array is an object of type Array . 这里的主要结论是,在javascript array中,是Array类型的对象 Actually defining array is pretty much the same as defining let a = { 0: 'one', 1: 'two' } except that Array (as a type) is recognized as "special" type in javascript that has special syntax for initialization ( let a = [ 'one', 'two', 'three' ] , gets zero-based numbered keys applied and it is iterable (can be used in for...of loops). Of course + all the prototype methods like find , filter , etc. Having this in mind adding string keyed property to an array is absolutely valid (not recommended) javascript and just because console.log applies special formatting when outputting arrays you get [ foo: 'one', bar: 'two' ] (with square brackets). 实际定义数组与定义let a = { 0: 'one', 1: 'two' }几乎相同,不同之处在于Array(作为一种类型)在具有特殊初始化语法的javascript中被识别为“特殊”类型( let a = [ 'one', 'two', 'three' ]获得基于零的数字键,并且它是可迭代的(可以用于循环的for...of )+所有原型方法,例如findfilter等。记住这一点,将字符串键控属性添加到数组是绝对有效的(不建议使用)javascript,并且仅仅因为console.log在输出数组时会应用特殊格式,所以您会得到[ foo: 'one', bar: 'two' ] (带方括号)。

You can even whip-up some very rough implementation of array yourself, like: https://jsfiddle.net/t96ehm1r/ (of course without any syntax sugar for quick initialization and all the prototype methods, etc.) 您甚至可以自己编写一些非常粗糙的数组实现,例如: https : //jsfiddle.net/t96ehm1r/ (当然,没有用于快速初始化的语法糖和所有原型方法,等等)。

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

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