You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`symbol`values are created by calling the `Symbol`constructor.
11
+
`symbol`值是通过调用 `Symbol`构造函数创建的。
12
12
13
13
```ts
14
14
let sym1 =Symbol();
15
15
16
-
let sym2 =Symbol("key"); //optional string key
16
+
let sym2 =Symbol("key"); //可选的字符串键
17
17
```
18
18
19
-
Symbols are immutable, and unique.
19
+
Symbol 不可变且唯一。
20
20
21
21
```ts
22
22
let sym2 =Symbol("key");
23
23
let sym3 =Symbol("key");
24
24
25
-
sym2===sym3; // false, symbols are unique
25
+
sym2===sym3; // false,symbol 唯一
26
26
```
27
27
28
-
Just like strings, symbols can be used as keys for object properties.
28
+
symbol 可以像字符串一样用作对象属性的键。
29
29
30
30
```ts
31
31
const sym =Symbol();
@@ -37,7 +37,7 @@ let obj = {
37
37
console.log(obj[sym]); // "value"
38
38
```
39
39
40
-
Symbols can also be combined with computed property declarations to declare object properties and class members.
40
+
symbol 也可以与计算属性声明结合使用,用于定义对象属性和类成员。
41
41
42
42
```ts
43
43
const getClassNameSymbol =Symbol();
@@ -54,25 +54,25 @@ let className = c[getClassNameSymbol](); // "C"
54
54
55
55
## `unique symbol`
56
56
57
-
To enable treating symbols as unique literals a special type `unique symbol` is available. `unique symbol`is a subtype of `symbol`, and are produced only from calling `Symbol()`or`Symbol.for()`, or from explicit type annotations. This type is only allowed on `const`declarations and `readonly static`properties, and in order to reference a specific unique symbol, you’ll have to use the `typeof`operator. Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.
57
+
为了能够将 symbol 视为唯一字面量,TypeScript 提供了一种特殊类型 `unique symbol`。`unique symbol`是 `symbol` 的子类型,仅能通过调用 `Symbol()`或`Symbol.for()`,或通过显式类型注解生成。此类型只能用于 `const`声明和 `readonly static`属性。要引用特定的唯一 symbol ,必须使用 `typeof`操作符。每个对唯一 symbol 的引用都意味着与给定声明关联的完全独特的身份。
58
58
59
59
```ts twoslash
60
60
// @errors: 1332
61
61
declareconst sym1:uniquesymbol;
62
62
63
-
// sym2 can only be a constant reference.
63
+
// sym2 只能是常量引用。
64
64
let sym2:uniquesymbol=Symbol();
65
65
66
-
//Works - refers to a unique symbol, but its identity is tied to 'sym1'.
In addition to user-defined symbols, there are well-known built-in symbols.
90
-
Built-in symbols are used to represent internal language behaviors.
89
+
除了用户自定义的 symbol 外,JavaScript 还提供了一些内置的著名 symbol。这些 symbol 用于表示语言的内部行为。
91
90
92
-
Here is a list of well-known symbols:
91
+
以下是著名 symbol 的列表:
93
92
94
93
### `Symbol.asyncIterator`
95
94
96
-
A method that returns async iterator for an object, compatible to be used with for await..of loop.
95
+
返回对象的异步迭代器的方法,适用于 `for await..of` 循环。
97
96
98
97
### `Symbol.hasInstance`
99
98
100
-
A method that determines if a constructor object recognizes an object as one of the constructor’s instances. Called by the semantics of the instanceof operator.
0 commit comments