简体   繁体   English

为什么 `Array.prototype.includes` 和 `Set.prototype.has` 的命名不同?

[英]Why is there a difference in the naming of `Array.prototype.includes` vs `Set.prototype.has`?

Both names seem to fit into the definition of "determine if a certain value is contained within this".这两个名称似乎都符合“确定某个值是否包含在其中”的定义。

Standard built-in objects that have these method names具有这些方法名称的标准内置对象

includes包括

  • Array大批
  • String细绳
  • TypedArray类型数组

has

  • Map Map
  • Set
  • WeakMap弱地图
  • WeakSet弱集

It seems it is the internal implementation which determines the name, I am interested if there is a specific reason why these have different naming conventions.似乎是内部实现决定了名称,如果有特定原因导致它们具有不同的命名约定,我很感兴趣。

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

It's a semantic difference on the nature of each function and their related parent.这是每个 function 及其相关父级的语义差异。 Maps and Sets only have one instance of what you check for.地图和集合只有一个您检查的实例。 That is why the categorical "has" verb is used.这就是使用分类“有”动词的原因。 Arrays and strings might contain multiple instances of what you ask includes to check, but it simply returns true at the first instance found, unaware of whether there are more instances of the looked-for object. Arrays 和字符串可能includes您要求检查的多个实例,但它只是在找到第一个实例时返回 true,不知道是否还有更多查找的 object 实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM