我是基本 JavaScript 概念的新手,所以想了解这是怎么可能的。
我有一个对象x
var x={}
它有一个属性y
,其值为一个数组
x.y=["a","b","c"]`
所以它就像一个 map ,其中 y
是键,数组["a","b","c"]
是值。那么如何 y
持有另一个名为 z
的属性,它有一些其他值,例如“hi”
x.y.z= "hi"
y
已经是 ["a","b","c"]
----------------- --------> x.y= ["a","b","c"]
怎么可能{"z":"hi"}
那么------------------------>x.y={"z":"hi"}
为了清楚地了解这一点,我应该熟悉哪些基本概念?
最佳答案
因为arrays是具有一些特殊方法的对象。
Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations. Neither the length of a JavaScript array nor the types of its elements are fixed. Since an array's size length grow or shrink at any time, JavaScript arrays are not guaranteed to be dense. In general, these are convenient characteristics; but if these features are not desirable for your particular use, you might consider using typed arrays.
Some people think that you shouldn't use an array as an associative array. In any case, you can use plain objects instead, although doing so comes with its own caveats. See the post Lightweight JavaScript dictionaries with arbitrary keys as an example.
关于Javascript 对象属性作为 map 的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35499195/