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]: update the translation of WeakMap constructor #16783

Merged
merged 3 commits into from
Nov 1, 2023
Merged
Changes from 1 commit
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
@@ -1,58 +1,58 @@
---
title: WeakMap() constructor
title: WeakMap() 构造函数
slug: Web/JavaScript/Reference/Global_Objects/WeakMap/WeakMap
---

{{JSRef}}

**`WeakMap()`** 会创建一个 `WeakMap` 对象,该对象是一组键/值对的集合,其中的键是弱引用的。其键必须是对象,而值可以是任意的。

你可以从这里了解更多关于 `WeakMap` 的内容 [WeakMap 对象](/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections)
**`WeakMap()`** 构造函数创建 {{jsxref("WeakMap")}} 对象。

## 语法

```js
new WeakMap();
new WeakMap([iterable]);
```js-nolint
new WeakMap()
new WeakMap(iterable)
```

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

## 参数

- `iterable`
- Iterable 是一个数组(二元数组)或者其他可迭代的且其元素是键值对的对象。每个键值对会被加到新的 WeakMap 里。null 会被当做 undefined。
- : 一个 [`Array`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array) 或者其他实现了 [`@@iterator`](/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/@@iterator) 方法的可迭代对象,该方法返回一个迭代器对象,该对象会产生一个二元数组对象,其第一个元素是将被用作 `WeakMap` 键的对象,第二个元素是与该键相关联的值。每个键值对都会被添加到新的 `WeakMap` 对象中。`null` 被视为 `undefined`
yin1999 marked this conversation as resolved.
Show resolved Hide resolved

## 示例

### 使用 `WeakMap`
### 使用 WeakMap

```JavaScript
const wm1 = new WeakMap(),
wm2 = new WeakMap(),
wm3 = new WeakMap();
const o1 = {},
o2 = function(){},
o3 = window;
```js
const wm1 = new WeakMap();
const wm2 = new WeakMap();
const wm3 = new WeakMap();
const o1 = {};
const o2 = function () {};
const o3 = window;

wm1.set(o1, 37);
wm1.set(o2, "azerty");
wm2.set(o1, o2); // value可以是任意值,包括一个对象或一个函数
wm2.set(o1, o2); // value 可以是任何值,包括对象或函数
wm2.set(o3, undefined);
wm2.set(wm1, wm2); // 键和值可以是任意对象,甚至另外一个WeakMap对象
wm2.set(wm1, wm2); // 键和值可以是任何对象,甚至是 WeakMap!

wm1.get(o2); // "azerty"
wm2.get(o2); // undefined,wm2中没有o2这个键
wm2.get(o3); // undefined,值就是undefined
wm2.get(o2); // undefined,因为 wm2 上没有 o2 的键
wm2.get(o3); // undefined,因为设置的值就是 undefined

wm1.has(o2); // true
wm2.has(o2); // false
wm2.has(o3); // true (即使值是undefined)
wm2.has(o3); // true(即使值本身是 ‘undefined’)
JasonLamv-t marked this conversation as resolved.
Show resolved Hide resolved

wm3.set(o1, 37);
wm3.get(o1); // 37

wm1.has(o1); // true
wm1.has(o1); // true
wm1.delete(o1);
wm1.has(o1); // false
wm1.has(o1); // false
```

## 规范
Expand All @@ -63,11 +63,11 @@ wm1.has(o1); // false

{{Compat}}

## 相关链接
## 参见

- A polyfill of `WeakMap` is available in [`core-js`](https://github.com/zloirock/core-js#weakmap)
- [`WeakMap` in the JavaScript guide](/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#weakmap对象)
- [Hiding Implementation Details with ECMAScript 6 WeakMaps](https://fitzgeraldnick.com/weblog/53/)
- [`core-js` 中 `WeakMap` 的 polyfill](https://github.com/zloirock/core-js#weakmap)
- [JavaScript 指南中的 `WeakMap`](/zh-CN/docs/Web/JavaScript/Guide/Keyed_collections#weakmap_object)
yin1999 marked this conversation as resolved.
Show resolved Hide resolved
- [使用 ECMAScript 6 WeakMap 隐藏实现细节](https://fitzgeraldnick.com/2014/01/13/hiding-implementation-details-with-e6-weakmaps.html)
- {{jsxref("Map")}}
- {{jsxref("Set")}}
- {{jsxref("WeakSet")}}