Looking this and this MDN pages it seems like the only difference between Maps and WeakMaps is a missing "size" property for WeakMaps. But is this true? What's the difference between them?

解决方案The experienced JavaScript programmer will notice that this API could

be implemented in JavaScript with two arrays (one for keys, one for

values) shared by the 4 API methods. Such an implementation would have

two main inconveniences. The first one is an O(n) search (n being the

number of keys in the map). The second one is a memory leak issue.

With manually written maps, the array of keys would keep references to

key objects, preventing them from being garbage collected. In native

WeakMaps, references to key objects are held "weakly", which means

that they do not prevent garbage collection in case there would be no

other reference to the object.

Because of references being weak, WeakMap keys are not enumerable

(i.e. there is no method giving you a list of the keys). If they were,

the list would depend on the state of garbage collection, introducing

non-determinism.

[And that's why they have no size property as well]

If you want to have a list of keys, you should

maintain it yourself. There is also an ECMAScript

proposal

aiming at introducing simple sets and maps which would not use weak

references and would be enumerable.

‐ which would be the "normal" Maps. Not mentioned at MDN, but in the harmony proposal, those also have items, keys and values generator methods and implement the Iterator interface.

map与weakmap,ES6 Map和WeakMap有什么区别?相关推荐

  1. ES6——Map和WeakMap

    ES6 提供了新的数据结构 Map. 它有九个常用方法: 通过键检查元素是否存在 has(key) 添加元素 set(key , value) 通过键移除元素 delete(key) 通过键获取值 g ...

  2. es6 Map,Set 和 WeakMap,WeakSet

    这些是新加的集合类型,提供了更加方便的获取属性值的方法,不用像以前一样用hasOwnProperty来检查某个属性是属于原型链上的呢还是当前对象的.同时,在进行属性值添加与获取时有专门的get,set ...

  3. ES6 系列之 WeakMap的使用示例

    ES6 系列之 WeakMap的使用示例 这篇文章主要介绍了ES6 系列之 WeakMap的使用示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 前言 我们先从 Wea ...

  4. ES6 Map数据结构

    Map JavaScript 的对象(Object),本质上是键值对的集合(Hash 结构),但是传统上只能用字符串当作键.这给它的使用带来了很大的限制. ES6 提供了 Map 数据结构.它类似于对 ...

  5. es6 Map、Set和Array.from()

    使用Map.Set.Array.from都需要支持es6的浏览器才能使用. 一.Map: 1.Map是一组键值对的结构,具有极快的查找速度. 2.格式类似于一个二维数组. var m = new Ma ...

  6. 【JavaScript进阶之旅 ES6篇 第十五章】Set、Map、Set跟Map跟Array跟Object的对比

    文章目录 一.Set 1.基本用法 2.Set.prototype 二.Map 1.基本用法 a. 键名为对象时 b. 普遍对象不能实现键值一一对应 c. Map数据结构实现键为对象的一一对应 d. ...

  7. es6 map()和filter()详解【转】

    本文转载自: https://www.cnblogs.com/zxyun/p/7019631.html 作者:zxyun 转载请注明该声明. 原文地址:http://www.zhangxinxu.co ...

  8. ES6 Map对象的使用

    转自: https://blog.csdn.net/u012664198/article/details/97373751 Map对象保存键值对,任何值(对象或者原始值) 都可以作为一个键或一个值 k ...

  9. es6 map与set

    set 定义: Set 对象允许你存储任何类型的唯一值,无论是原始值或者是对象引用,Set对象是值的集合,你可以按照插入的顺序迭代它的元素.Set中的元素只会出现一次,即 Set 中的元素是唯一的 S ...

最新文章

  1. servlet生命周期
  2. 多线程:Executor、Sleep、Deamon、Yeild
  3. 【视频课】零基础免费38课时深度学习+超60小时CV核心算法+15大Pytorch CV实践案例助你攻略CV...
  4. 使用Docker Compose 部署Nexus后提示:Unable to create directory /nexus-data/instance
  5. linux 切换用户_Linux 用户态切换到内核态的 3 种方式
  6. SAP UI5 oFileUpload.getUploadEnabled()
  7. 反射在java中的应用_java反射机制在项目中的运用
  8. linux socket 编程(C语言)
  9. 作者:朱扬勇(1963-),男,博士,复旦大学计算机科学技术学院教授、学术委员会主任,上海市数据科学重点实验室主任。...
  10. mysql新增范围之外数据_mysql第二天 数据的增删改查补充及外键
  11. 5分钟就能完成的Python小项目,赶紧拿去玩玩吧
  12. 公有云 私有云 混合云_您需要了解的有关混合云的知识
  13. linux串口查看mxser,Ubuntu8.04安装CP-168U的串口连接问题
  14. 用PHP调用WEBSERVICE
  15. redis 是哪个厂家的_redis 基本数据类型-字符串(String)
  16. SharePoint Server 2010 LDAP Form 验证最容易出错的几点
  17. 较为详细的MUSIC算法原理及MATLAB实现
  18. 谈谈平安京资源的npk解包思路
  19. [hive 报错]:FAILED:SemanticException [Error 10025] Expression not in GROUP BY key
  20. 2022最火的Linux性能分析工具--perf

热门文章

  1. 编写C语言代码,实现以下功能:有N名学生,每个人的信息包括学号、姓名和语文、数学、英语、平均分等内容(用结构体表示 ),输出平均分排名倒数第2的学生信息。
  2. 发动机压缩比怎么计算公式_怎么判断发动机有积碳,发动机积碳多的症状有哪些...
  3. Arrays类详细讲解
  4. 软件数控编程_这么多CNC数控编程软件, 你觉得哪个好?
  5. matlab生成西门子plc源文件,西门子PLC如何把DB快做成源文件
  6. docker查找镜像_5 款非常好用的开源 Docker 工具,get一波~
  7. 安卓手机qq怎么看密友值_qq亲密关系分数值是什么在哪看 怎么查情侣值或闺蜜值方法...
  8. python 读取地震道头数据_python地震数据可视化详解
  9. 算法的时间复杂度和空间复杂度的原理
  10. SQL2012(32位)下载及安装(32位、64位都一样,不影响)