javascript node.js 数组实际上是哈希图吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23610105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are node.js arrays actually hashmaps?
提问by Joel
To my surprise, this code actually works in node.js:
令我惊讶的是,这段代码实际上适用于 node.js:
var arr = new Array();
// also works: var arr = [];
arr[0] = 123;
arr['abc'] = 456;
arr; // node.js: [ 123, abc: 456 ], chrome: [123]
I've always thought that an array stores its objects in order, only accessible by an integer key, like a std::vector in C++. However, here it's acting like a map or an object. Adding to the confusion, the same code works as expected in chrome, returning an array with a single entry, 123. I thought node.js and chrome javascript use the same internal engine, V8. What's going on here?
我一直认为数组按顺序存储其对象,只能通过整数键访问,就像 C++ 中的 std::vector 一样。然而,在这里它就像一张地图或一个对象。更令人困惑的是,相同的代码在 chrome 中按预期工作,返回一个包含单个条目 123 的数组。我认为 node.js 和 chrome javascript 使用相同的内部引擎 V8。这里发生了什么?
回答by Carlos
Javascript allows you to extend objects on the fly, and as an Array
is an object you can do so.
JavaScript允许你扩展的物体在飞行中,并作为Array
是一个对象,你可以这样做。
What you are doing there is adding a new property to your array called abc
and assigning it the value 456
.
你在那里做的是向你的数组添加一个新属性,abc
并为其分配 value 456
。
So you could say every object in Javascript can be used as a hashmap somehow.
所以你可以说 Javascript 中的每个对象都可以以某种方式用作哈希图。
EDIT
编辑
It seems that Chrome filters the non-numeric properties of the Array
object at dumping whilst Node dumps every user-defined property. In my opinion Node's way is better since the alpha-numeric property is available in a for in
statement:
似乎 ChromeArray
在转储时过滤了对象的非数字属性,而 Node 会转储每个用户定义的属性。在我看来,Node 的方式更好,因为 alpha-numeric 属性在for in
语句中可用:
var a = [1];
a['abc'] = 2;
for (var i in a) {
console.log(i);
}
// Prints:
// 0
// abc
回答by Miroslav Mocek
The answers are right, the behaviour is maybe more understandable, if you try to display lengthof the array.
答案是正确的,如果您尝试显示数组的长度,则行为可能更容易理解。
var ar = []
ar[0] = 42
console.log(ar.length) // 1
ar[12] = 21
console.log(ar.length) // 13
ar['ab'] = 1
console.log(ar.length) // 13 (separate property, not in array)
ar[ar.length] = 33
console.log(ar.length) // 14
ar.push(55)
console.log(ar.length) // 15
console.log(ar) // display all items specified above
//[ 42, , , , , , , , , , , , 21, 33, 55, ab: 1 ]
// which in fact really is:
// [ 42, , , , , , , , , , , , 21, 33, 55] as array and
// special property of array object - 'ab':1
回答by Cerbrus
While chrome doesn't show 456
in the console when you just enter arr
, arr.abc
will still be 456
.
虽然 chrome456
在您刚输入时没有显示在控制台中arr
,arr.abc
但仍会是456
.
It just doesn't show it in the console unless you explicitly access the variable, or console.log(arr)
, which logs: [123, abc: 456]
.
它只是不会在控制台中显示它,除非您显式访问变量或console.log(arr)
,它记录:[123, abc: 456]
。
Basically, this is just a cosmetic issue. Node.js does show key/value properties on array objects, when you just enter the variable in the console, while chrome only shows "normal" array entries, even though both arrays actually have the same contents.
基本上,这只是一个表面问题。Node.js 确实在数组对象上显示键/值属性,当您在控制台中输入变量时,而 chrome 仅显示“正常”数组条目,即使两个数组实际上具有相同的内容。