Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
164 views
in Technique[技术] by (71.8m points)

javascript - JavaScript中的地图与对象(Map vs Object in JavaScript)

I just discovered chromestatus.com and, after losing several hours of my day, found this feature entry :

(我刚发现chromestatus.com,在失去了一天的时间后,发现了以下功能条目 :)

Map: Map objects are simple key/value maps.

(映射:映射对象是简单的键/值映射。)

That confused me.

(那让我感到困惑。)

Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary?

(常规JavaScript对象是字典,那么Map与字典有何不同?)

Conceptually, they're identical (according to What is the difference between a Map and a Dictionary? )

(从概念上讲,它们是相同的(根据Map和Dictionary之间的区别是什么? ))

The documentation chromestatus references doesn't help either:

(chromestatus文档参考无济于事:)

Map objects are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript language values.

(映射对象是键/值对的集合,其中键和值都可以是任意ECMAScript语言值。)

A distinct key value may only occur in one key/value pair within the Map's collection.

(一个唯一的键值只能出现在地图集合中的一个键/值对中。)

Distinct key values as discriminated using the a comparision algorithm that is selected when the Map is created.

(使用创建地图时选择的比较算法来区分的不同键值。)

A Map object can iterate its elements in insertion order.

(Map对象可以按插入顺序迭代其元素。)

Map object must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection.

(必须使用哈希表或其他机制来实现Map对象,这些机制通常提供的访问时间与集合中元素的数量成线性关系。)

The data structures used in this Map objects specification is only intended to describe the required observable semantics of Map objects.

(本Map对象规范中使用的数据结构仅用于描述Map对象所需的可观察语义。)

It is not intended to be a viable implementation model.

(它并非旨在成为可行的实施模型。)

…still sounds like an object to me, so clearly I've missed something.

(…对我来说听起来还是一个对象,所以很明显我错过了一些东西。)

Why is JavaScript gaining a (well-supported) Map object?

(为什么JavaScript会获得一个(得到良好支持的) Map对象?)

What does it do?

(它有什么作用?)

  ask by Dave translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

According to mozilla:

(根据mozilla:)

A Map object can iterate its elements in insertion order - a for..of loop will return an array of [key, value] for each iteration.

(Map对象可以按插入顺序对其元素进行迭代-for..of循环将为每次迭代返回[key,value]数组。)

and

(和)

Objects are similar to Maps in that both let you set keys to values, retrieve those values, delete keys, and detect whether something is stored at a key.

(对象与Maps相似,两者都可以让您将键设置为值,检索这些值,删除键并检测是否在键处存储了某些内容。)

Because of this, Objects have been used as Maps historically;

(因此,对象在历史上一直被用作地图;)

however, there are important differences between Objects and Maps that make using a Map better.

(但是,对象和地图之间存在重要差异,这使得使用地图更好。)

An Object has a prototype, so there are default keys in the map.

(对象具有原型,因此地图中包含默认键。)

However, this can be bypassed using map = Object.create(null).

(但是,可以使用map = Object.create(null)绕过此操作。)

The keys of an Object are Strings, where they can be any value for a Map.

(对象的键是字符串,其中键可以是Map的任何值。)

You can get the size of a Map easily while you have to manually keep track of size for an Object.

(您可以轻松获取地图的大小,而不必手动跟踪对象的大小。)

Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.

(当键直到运行时才是未知的,并且所有键都是同一类型并且所有值都是同一类型时,请在对象上使用映射。)

Use objects when there is logic that operates on individual elements.

(当存在对单个元素进行操作的逻辑时,请使用对象。)

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map)

The iterability-in-order is a feature that has long been wanted by developers, in part because it ensures the same performance in all browsers.

(顺序可迭代性是开发人员长期以来一直希望使用的功能,部分原因是它确保了所有浏览器的相同性能。)

So to me that's a big one.

(所以对我来说,那是很大的。)

The myMap.has(key) method will be especially handy, and also the myMap.size property.

(myMap.has(key)方法以及myMap.size属性将特别方便。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...