映射与对象在JavaScript [英] Map vs Object in JavaScript

查看:97
本文介绍了映射与对象在JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚发现了chromestatus.com,在我一天失去了几个小时后,发现了此功能条目

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


地图:地图对象是简单的键/值映射。

Map: Map objects are simple key/value maps.

这让我很困惑常规JavaScript对象是字典,所以与字典不同的是 Map 从概念上讲,它们是一样的(根据什么是地图和字典之间的区别?

That confused me. Regular JavaScript objects are dictionaries, so how is a Map different from a dictionary? Conceptually, they're identical (according to What is the difference between a Map and a Dictionary?)

文档chromestatus引用不能帮助:

The documentation chromestatus references doesn't help either:


映射对象是键/值对的集合,其中键和值都可以是任意的ECMAScript语言值。唯一的键值可能仅在地图集合中的一个键/值对中发生。使用在创建地图时选择的比较算法来区分不同的键值。

Map objects are collections of key/value pairs where both the keys and values may be arbitrary ECMAScript language values. 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.

Map对象可以以插入顺序迭代其元素。 Map对象必须使用哈希表或其他机制来实现,平均来说,这些机制提供了对集合中元素数量进行子线性访问的时间。此Map对象规范中使用的数据结构仅用于描述Map对象所需的可观察语义。它不是一个可行的实现模型。

A Map object can iterate its elements in insertion order. 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. The data structures used in this Map objects specification is only intended to describe the required observable semantics of Map objects. It is not intended to be a viable implementation model.

...仍然听起来像是一个对象,显然我错过了一些东西。

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

为什么JavaScript获得(良好支持) Map 对象?它是做什么的?

Why is JavaScript gaining a (well-supported) Map object? What does it do?

推荐答案

根据mozilla:


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

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


对象类似于地图,可以让您将键设置为值,
检索这些值,删除键,并检测某个东西是否存储在一个键上的
。因此,对象已被用作历史上的
;然而,对象
和地图之间存在重要的区别,使得更好地使用地图。

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. 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.

对象具有原型,因此地图中有默认键。
但是,这可以通过使用map = Object.create(null)来绕过。对象的
键是字符串,它们可以是Map的任何值。
您可以轻松获取地图的大小,而您必须手动保留
跟踪对象的大小。

An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = Object.create(null). The keys of an Object are Strings, where they can be any value for a 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/zh-CN/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.

myMap.has(key)方法将会特别方便,还有 myMap.size 属性。

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

这篇关于映射与对象在JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆