为什么Set中的项目在JavaScript中被称为值而不是键? [英] Why item in Set is called value instead of key in JavaScript?

查看:88
本文介绍了为什么Set中的项目在JavaScript中被称为值而不是键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Set.prototype.forEach 接受ES6中的回调函数该函数有3个参数与 Map.prototype.forEach一致 MDN 说前两个回调的参数是值,因为 Set 对象中没有键。为什么 Set 中的项目被视为值而不是键? Set 对象中没有重复的键会更有意义,因为 Map 中也没有重复的键。

Set.prototype.forEach which accepts a callback function in ES6 The function has 3 parameters to be consistent with Map.prototype.forEach. MDN says both the first 2 parameters for the callback are values because there is no keys in Set object. Why the item in Set is considered as value instead of key? No duplicated key in Set object would make more sense because there is also no duplicated key in Map.

Set 中的值看起来像 Map 中的键的另一个方面是它使 Set.prototype.has(value) Map.prototype.has(key)更加一致。 数组(键是索引)和 Map 中不允许使用重复键,但这不能扩展到设置,它不允许重复的值。使其成为所有容器中不允许重复的密钥可能是一个更简洁的规则。

Another aspect that value in Set looks like key in Map is that it makes Set.prototype.has(value) and Map.prototype.has(key) more consistent. Duplicated keys are NOT allowed in Array (key is index) and Map, but this cannot be extended to Set which doesn't allow duplicated values. Make it as duplicated keys are not allowed in all containers might be a more succinct rule.

推荐答案

从您拥有的MDN链接附带。

From the MDN link which you have attached.


Set对象中没有键。但是,前两个参数是
包含在Set中的两个值,因此回调函数是
,与map和Array的forEach方法一致。

The are no keys in Set objects. However, the first two arguments are both values contained in the Set, so that the callback function is consistent with the forEach methods for Map and Array.

我们可以说set只是一个保持不同元素的数组。套装中没有钥匙。

We can say that set is just an array which keeps distinct elements. There are no keys in sets.

这篇关于为什么Set中的项目在JavaScript中被称为值而不是键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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