From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/set/foreach/index.html | 145 +++++++++++++++++++++ 1 file changed, 145 insertions(+) create mode 100644 files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html (limited to 'files/zh-cn/web/javascript/reference/global_objects/set/foreach') diff --git a/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html b/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html new file mode 100644 index 0000000000..e89be3af36 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/set/foreach/index.html @@ -0,0 +1,145 @@ +--- +title: Set.prototype.forEach() +slug: Web/JavaScript/Reference/Global_Objects/Set/forEach +translation_of: Web/JavaScript/Reference/Global_Objects/Set/forEach +--- +
{{JSRef}}
+ +

forEach 方法会根据集合中元素的插入顺序,依次执行提供的回调函数。

+ +

语法

+ +
mySet.forEach(callback[, thisArg])
+ +

参数

+ +
+
callback
+
为集合中每个元素执行的回调函数,该函数接收三个参数:
+
+
+
currentValuecurrentKey{{optional_inline}}
+
currentValue 是正在被操作的元素。并且由于集合没有索引,所以 currentKey 也表示这个正在被操作的元素。
+
set{{optional_inline}}
+
调用当前 forEach 方法的集合对象
+
+
+
thisArg{{optional_inline}}
+
回调函数执行过程中的 this 值。
+
+ +

返回值

+ +

{{jsxref("undefined")}}.

+ +

描述

+ +

forEach 方法会依次为集合中的元素执行回调函数,就算元素的值是 undefined

+ +

回调函数有三个参数:

+ + + +

但是由于集合对象中没有索引(keys),所以前两个参数都是{{domxref("Set")}}中元素的值(values),之所以这样设计回调函数是为了和{{jsxref("Map.foreach", "Map")}} 以及{{jsxref("Array.forEach","Array")}}的 forEach 函数用法保持一致。

+ +

如果提供了一个 thisArg 参数给 forEach 函数,则参数将会作为回调函数中的 this值。否则 this 值为 undefined。回调函数中 this 的绑定是根据函数被调用时通用的 this 绑定规则来决定的

+ +

forEach 函数为集合对象中每个元素都执行一次回调;它不会返回任何值。

+ +

例子

+ +

输出集合对象的内容

+ +

以下代码依次打印集合对象的元素:

+ +
function logSetElements(value1, value2, set) {
+    console.log("s[" + value1 + "] = " + value2);
+}
+
+new Set(["foo", "bar", undefined]).forEach(logSetElements);
+
+// logs:
+// "s[foo] = foo"
+// "s[bar] = bar"
+// "s[undefined] = undefined"
+
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-set.prototype.foreach', 'Set.prototype.forEach')}}{{Spec2('ES6')}}Initial definition.
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{ CompatChrome(38) }}{{CompatGeckoDesktop("25.0")}}{{ CompatIE("11") }}257.1
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatNo}}38{{CompatGeckoMobile("25.0")}}{{CompatNo}}{{CompatNo}}8
+
+ +

参见

+ + -- cgit v1.2.3-54-g00ecf