【问题标题】:Why is there a difference in the naming of `Array.prototype.includes` vs `Set.prototype.has`?为什么 `Array.prototype.includes` 和 `Set.prototype.has` 的命名不同?
【发布时间】:2020-09-06 08:17:14
【问题描述】:

这两个名称似乎都符合“确定某个值是否包含在其中”的定义。

具有这些方法名称的标准内置对象

包括

  • 数组
  • 字符串
  • 类型化数组

  • 地图
  • 设置
  • 弱地图
  • 弱集

似乎是内部实现决定了名称,如果有特定原因导致它们具有不同的命名约定,我很感兴趣。

const array = ["a", "b", "c"];
const string = "abc";
string.includes("a"); // true
array.includes("a"); // true

const set = new Set(["a", "b", "c"])
const map = new Map([["a", "a"], ["b", "b"], ["c", "c"]]);
set.has("a"); // true
map.has("a"); // true

【问题讨论】:

    标签: javascript ecmascript-6 naming-conventions


    【解决方案1】:

    这是每个函数及其相关父函数的性质的语义差异。地图和集合只有一个您检查的实例。这就是使用分类“有”动词的原因。数组和字符串可能包含您要求 includes 检查的多个实例,但它只是在找到第一个实例时返回 true,不知道是否还有更多查找对象的实例。

    【讨论】:

      猜你喜欢
      • 2013-07-16
      • 2014-10-03
      • 1970-01-01
      • 2021-06-01
      • 1970-01-01
      • 2019-11-14
      • 1970-01-01
      • 2014-03-07
      相关资源
      最近更新 更多