Skip to content
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

[zh-cn]: add the translation of WeakSet constructor #16827

Merged
merged 3 commits into from
Nov 5, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
title: WeakSet() 构造函数
slug: Web/JavaScript/Reference/Global_Objects/WeakSet/WeakSet
---

{{JSRef}}

**`WeakSet()`** 构造函数创建 {{jsxref("WeakSet")}} 对象。

## 语法

```js-nolint
new WeakSet()
new WeakSet(iterable)
```

> **备注:** `WeakSet()` 构造函数只能使用 [`new`](/zh-CN/docs/Web/JavaScript/Reference/Operators/new) 调用。不使用 `new` 而直接调用会抛出 {{jsxref("TypeError")}}。

### 参数

- `iterable` {{optional_inline}}
- : 如果传入了一个[可迭代对象](/zh-CN/docs/Web/JavaScript/Reference/Statements/for...of),这个对象的所有元素都会被添加到新的 `WeakSet` 对象中。`null` 会被视为 `undefined`。

## 示例

### 使用 WeakSet 对象

```js
const ws = new WeakSet();
const foo = {};
const bar = {};

ws.add(foo);
ws.add(bar);

ws.has(foo); // true
ws.has(bar); // true

ws.delete(foo); // 将 foo 从集合中移除
ws.has(foo); // false,foo 已经被移除
ws.has(bar); // true,bar 被保留
```

请注意 `foo !== bar`。虽然它们是相似的对象,_但它们**不是同一个对象**_。因此,它们都被添加到集合中。
JasonLamv-t marked this conversation as resolved.
Show resolved Hide resolved

## 规范

{{Specifications}}

## 浏览器兼容性

{{Compat}}

## 参见

- [`core-js` 中 `WeakSet` 的 polyfill](https://github.com/zloirock/core-js#weakset)
- {{jsxref("WeakSet")}}