aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/symbol/description/index.html
blob: 1e52358a632bba0890239299ba1f6c2e396b8600 (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
---
title: Symbol.prototype.description
slug: Web/JavaScript/Reference/Global_Objects/Symbol/description
tags:
  - JavaScript
  - Property
  - Prototype
  - Symbol
translation_of: Web/JavaScript/Reference/Global_Objects/Symbol/description
---
<div>{{JSRef}}</div>

<p><code><strong>description</strong></code> 是一个只读属性,它会返回 {{jsxref("Symbol")}} 对象的可选描述的字符串。</p>

<div>{{EmbedInteractiveExample("pages/js/symbol-prototype-description.html")}}</div>



<h2 id="语法">语法</h2>

<pre class="syntaxbox">Symbol('myDescription').description;
Symbol.iterator.description;
Symbol.for('foo').description;
</pre>

<h2 id="描述">描述</h2>

<p>{{jsxref("Symbol")}} 对象可以通过一个可选的描述创建,可用于调试,但不能用于访问 symbol 本身。<code>Symbol.prototype.description</code> 属性可以用于读取该描述。与 <code>Symbol.prototype.toString()</code> 不同的是它不会包含 "<code>Symbol()</code>" 的字符串。具体请看实例。</p>

<h2 id="实例">实例</h2>

<pre class="brush: js">Symbol('desc').toString();   // "Symbol(desc)"
Symbol('desc').description;  // "desc"
Symbol('').description;      // ""
Symbol().description;        // undefined

// well-known symbols
Symbol.iterator.toString();  // "Symbol(Symbol.iterator)"
Symbol.iterator.description; // "Symbol.iterator"

// global symbols
Symbol.for('foo').toString();  // "Symbol(foo)"
Symbol.for('foo').description; // "foo"

</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
  </tr>
  <tr>
   <td><code><a href="https://tc39.github.io/proposal-Symbol-description/#sec-symbol.prototype.description">get Symbol.prototype.description</a></code> proposal</td>
   <td>Stage 3</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容">浏览器兼容</h2>



<p>{{Compat("javascript.builtins.Symbol.description")}}</p>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li>{{jsxref("Symbol.prototype.toString()")}}</li>
 <li>Polyfill: <a href="https://npmjs.com/symbol.prototype.description">https://npmjs.com/symbol.prototype.description</a></li>
</ul>