-
Notifications
You must be signed in to change notification settings - Fork 0
/
13-5.html
128 lines (118 loc) · 4.05 KB
/
13-5.html
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="icon" href="./public/favicon.ico" />
<meta http-equiv="cache-control" content="no-cache" />
<title></title>
<link rel="stylesheet" href=" https://necolas.github.io/normalize.css/8.0.1/normalize.css" />
<link rel="stylesheet" href="./hightlight/default.min.css" />
<link rel="stylesheet" href="./css/main.css" />
<link rel="stylesheet" href="./css/copybutton.css" />
<link rel="stylesheet" href="./css/hightlight.css" />
<script src="./hightlight/hightlight.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.11/clipboard.min.js"></script>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-BEVZJDBC7Z"></script>
<script src="./js/gtag.js"></script>
</head>
<body>
<header>
<nav>
<h1>
<span id="toggle-menu"></span>
<a href="index.html"></a>
</h1>
</nav>
</header>
<main>
<aside>
<nav></nav>
</aside>
<article>
<h2 id="13-5">13-5 Symbol.prototype.description</h2>
<p>
在 ES10 之前,我們知道 Symbol
的描述只是通過構建構子傳入,並不能直接獲取到。如果我們要獲取,只能通過 toString 轉換
</p>
<p>方法定義:</p>
<pre><code class="language-ts">
interface SymbolConstructor {
/**
* Returns a new unique Symbol value.
* @param description Description of the new Symbol object.
*/
(description?: string | number): symbol;
}
</code></pre>
<pre><code class="language-js ">
const hi = Symbol("hi");
console.log(hi.toString());
// Symbol(hi)
console.log(hi.toString() === "Symbol(hi)");
// true
</code></pre>
<p>現在可以直接獲取</p>
<pre><code class="language-ts">
interface Symbol {
/**
* Expose the [[Description]] internal slot of a symbol directly.
*/
readonly description: string | undefined;
}
</code></pre>
<pre><code class="language-js ">
const hi = Symbol("hi");
console.log(hi.description);
// hi
console.log(hi.description === "hi");
// true
</code></pre>
<p>其他示範</p>
<pre><code class="language-js">
console.log(Symbol('desc').description);
// Expected output: "desc"
console.log(Symbol.iterator.description);
// Expected output: "Symbol.iterator"
console.log(Symbol.for('foo').description);
// Expected output: "foo"
console.log(`${Symbol('foo').description}bar`);
// Expected output: "foobar"
</code></pre>
<p>
Symbol 物件可以通過一個可選的描述創建,可用於調試,但不能用於訪問 symbol
本身。Symbol.prototype.description 屬性可以用於讀取該描述。與 Symbol.prototype.toString()
不同的是它不會包含 "Symbol()" 的字串。具體請看下列範例。
</p>
<pre><code class="language-js">
Symbol("desc").toString(); // "Symbol(desc)"
Symbol("desc").description; // "desc"
Symbol("").description; // ""
Symbol().description; // undefined
// 內建通用 (well-known) symbol
Symbol.iterator.toString(); // "Symbol(Symbol.iterator)"
Symbol.iterator.description; // "Symbol.iterator"
// global symbols
Symbol.for("foo").toString(); // "Symbol(foo)"
Symbol.for("foo").description; // "foo"
</code></pre>
<p>資料來源:</p>
<p>
<a
href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/description"
>
https://developer.mozilla.org/.../Symbol/description
</a>
</p>
<p>
<a href="https://juejin.cn/post/7023895016384757796">
https://juejin.cn/post/7023895016384757796
</a>
</p>
</article>
</main>
</body>
<script type="module" src="./js/main.js"></script>
</html>