blob: 9643d66bfd050d5181161ece8243853762160c33 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
---
title: WeakMap.prototype.set()
slug: Web/JavaScript/Reference/Global_Objects/WeakMap/set
tags:
- JavaScript
- Method
- WeakMap
translation_of: Web/JavaScript/Reference/Global_Objects/WeakMap/set
---
<div>{{JSRef}}</div>
<p><code><strong>set()</strong></code> 方法根据指定的<code>key</code>和<code>value在</code> <code>WeakMap</code>对象中添加新/更新元素。</p>
<div>{{EmbedInteractiveExample("pages/js/weakmap-prototype-set.html")}}</div>
<h2 id="语法">语法</h2>
<pre class="syntaxbox"><em>wm</em>.set(key, value);</pre>
<h3 id="参数">参数</h3>
<dl>
<dt>key</dt>
<dd>必须的。必须是<code>对象</code>。是要在<code>WeakMap</code> 对象中添加元素的key部分。</dd>
<dt>value</dt>
<dd>必须的。任意的值。是要在<code>WeakMap</code> 对象中添加/元素的value部分。</dd>
</dl>
<h3 id="返回值">返回值</h3>
<p>该<code>WeakMap</code>对象</p>
<h2 id="例子">例子</h2>
<h3 id="使用set方法"><code>使用set方法</code></h3>
<pre class="brush: js">var wm = new WeakMap();
var obj = {};
// Add new elements to the WeakMap
wm.set(obj, "foo").set(window, "bar"); // chainable
// Update an element in the WeakMap
wm.set(obj, "baz");
</pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td>{{SpecName('ES6', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}}</td>
<td>{{Spec2('ES6')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#sec-weakmap.prototype.set', 'WeakMap.prototype.set')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td></td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容">浏览器兼容</h2>
<p>{{Compat("javascript.builtins.WeakMap.set")}}</p>
<h2 id="Firefox-特殊说明">Firefox-特殊说明</h2>
<ul>
<li>直到Firefox 33 {{geckoRelease("33")}}, <code>WeakMap.prototype.set</code> returned <code>undefined</code> 而不支持联式。它已经修复 ({{bug(1031632)}}). 这样的实现也存在于 Chrome/v8 (<a href="https://code.google.com/p/v8/issues/detail?id=3410">issue</a>).</li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
<li>{{jsxref("WeakMap")}}</li>
<li>{{jsxref("WeakMap.prototype.get()")}}</li>
<li>{{jsxref("WeakMap.prototype.has()")}}</li>
</ul>
|