-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set, WeakSet, Map, WeakMap #2
Comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Set
Set 是类数组结构,和数组的区别是,Set 中的元素都是唯一的,不能出现重复元素。
Set 的创建
Set 构造函数可以接收一个参数,这个参数可以是数组,字符串等一切具有 iterable 接口的其他数据结构
Set 的实例属性
Set 的实例方法
Set 的示例方法有两大类:操作方法和遍历方法
keys, values, entries 方法返回的都是遍历器对象
Set 结构的实例默认可遍历,它的默认遍历器生成函数就是它的 values 方法。这意味着,可以省略 values 方法,直接用 for...of 循环遍历 Set
数组去重
并集(Union)、交集(Intersect)和差集(Difference)
WeakSet
WeakSet 结构与 Set 类似,也是不重复的值的集合。但是,它与 Set 有两个区别:
Set 的创建
WeakSet 的实例方法
WeakSet 没有 size 属性,也不可便利
Map
类似于对象,也是键值对的集合,但是“键”的范围不限于字符串,各种类型的值(包括对象)都可以当作键
Map 语法
任何具有 Iterator 接口、且每个成员都是一个双元素的数组的数据结构都可以当作 Map 构造函数的参数
如果对同一个键多次赋值,后面的值将覆盖前面的值。
如果读取一个未知的键,则返回 undefined。
如果 Map 的键是一个简单类型的值,则只要两个值严格相等,Map 将其视为一个键。NaN 不严格相等于自身,但 Map 将其视为同一个键。
实例属性和方法
遍历方法
使用扩展运算符将 Map 转数组
WeakMap
WeakMap 结构与 Map 结构类似,也是用于生成键值对的集合。
WeakMap 与 Map 的区别:
WeakMap 只接受对象作为键名(null 除外),不接受其他类型的值作为键名
WeakMap 的键名所指向的对象,不计入垃圾回收机制
WeakMap 没有遍历操作(即没有 keys()、values()和 entries()方法),也没有 size 属性
WeakMap 无法清空,即不支持 clear 方法
WeakMap 弱引用的只是键名,而不是键值。键值依然是正常引用
语法
实例方法
The text was updated successfully, but these errors were encountered: