Skip to content

Commit 2b5e4a6

Browse files
init translation of symbols (#91)
* init translation of symbols * Revised translation of Symbols * Apply suggestions from code review Co-authored-by: Xavi Lee <[email protected]> * Apply suggestions from code review * update --------- Co-authored-by: Xavi Lee <[email protected]>
1 parent 539346d commit 2b5e4a6

File tree

1 file changed

+29
-33
lines changed

1 file changed

+29
-33
lines changed
Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
---
2-
title: Symbols
2+
title: Symbol
33
layout: docs
44
permalink: /zh/docs/handbook/symbols.html
5-
oneline: Using the JavaScript Symbol primitive in TypeScript
5+
oneline: 在 TypeScript 中使用 JavaScript Symbol 原始数据类型
66
translatable: true
77
---
88

9-
Starting with ECMAScript 2015, `symbol` is a primitive data type, just like `number` and `string`.
9+
ECMAScript 2015 开始,`symbol` 成为一种基本数据类型,类似于 `number` `string`
1010

11-
`symbol` values are created by calling the `Symbol` constructor.
11+
`symbol` 值是通过调用 `Symbol` 构造函数创建的。
1212

1313
```ts
1414
let sym1 = Symbol();
1515

16-
let sym2 = Symbol("key"); // optional string key
16+
let sym2 = Symbol("key"); // 可选的字符串键
1717
```
1818

19-
Symbols are immutable, and unique.
19+
Symbol 不可变且唯一。
2020

2121
```ts
2222
let sym2 = Symbol("key");
2323
let sym3 = Symbol("key");
2424

25-
sym2 === sym3; // false, symbols are unique
25+
sym2 === sym3; // false,symbol 唯一
2626
```
2727

28-
Just like strings, symbols can be used as keys for object properties.
28+
symbol 可以像字符串一样用作对象属性的键。
2929

3030
```ts
3131
const sym = Symbol();
@@ -37,7 +37,7 @@ let obj = {
3737
console.log(obj[sym]); // "value"
3838
```
3939

40-
Symbols can also be combined with computed property declarations to declare object properties and class members.
40+
symbol 也可以与计算属性声明结合使用,用于定义对象属性和类成员。
4141

4242
```ts
4343
const getClassNameSymbol = Symbol();
@@ -54,25 +54,25 @@ let className = c[getClassNameSymbol](); // "C"
5454

5555
## `unique symbol`
5656

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 的引用都意味着与给定声明关联的完全独特的身份。
5858

5959
```ts twoslash
6060
// @errors: 1332
6161
declare const sym1: unique symbol;
6262

63-
// sym2 can only be a constant reference.
63+
// sym2 只能是常量引用。
6464
let sym2: unique symbol = Symbol();
6565

66-
// Works - refers to a unique symbol, but its identity is tied to 'sym1'.
66+
// 正常工作——引用一个唯一 symbol ,但其身份与‘sym1’绑定。
6767
let sym3: typeof sym1 = sym1;
6868

69-
// Also works.
69+
// 也正常工作。
7070
class C {
7171
static readonly StaticSymbol: unique symbol = Symbol();
7272
}
7373
```
7474

75-
Because each `unique symbol` has a completely separate identity, no two `unique symbol` types are assignable or comparable to each other.
75+
由于每个 `unique symbol` 具有完全独立的身份,因此没有两个 `unique symbol` 类型可以互相赋值或比较。
7676

7777
```ts twoslash
7878
// @errors: 2367
@@ -84,60 +84,56 @@ if (sym2 === sym3) {
8484
}
8585
```
8686

87-
## Well-known Symbols
87+
## 著名 symbol
8888

89-
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 用于表示语言的内部行为。
9190

92-
Here is a list of well-known symbols:
91+
以下是著名 symbol 的列表:
9392

9493
### `Symbol.asyncIterator`
9594

96-
A method that returns async iterator for an object, compatible to be used with for await..of loop.
95+
返回对象的异步迭代器的方法,适用于 `for await..of` 循环。
9796

9897
### `Symbol.hasInstance`
9998

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.
99+
判断一个构造函数对象是否将某个对象视为该构造函数的实例的方法。由 `instanceof` 操作符的语义调用。
101100

102101
### `Symbol.isConcatSpreadable`
103102

104-
A Boolean value indicating that an object should be flattened to its array elements by Array.prototype.concat.
103+
一个布尔值,指示对象是否应该在 `Array.prototype.concat` 中被展平为其数组元素。
105104

106105
### `Symbol.iterator`
107106

108-
A method that returns the default iterator for an object. Called by the semantics of the for-of statement.
107+
返回对象的默认迭代器的方法。由 `for-of` 语句的语义调用。
109108

110109
### `Symbol.match`
111110

112-
A regular expression method that matches the regular expression against a string. Called by the `String.prototype.match` method.
111+
一个正则表达式方法,将正则表达式与字符串进行匹配。由 `String.prototype.match` 方法调用。
113112

114113
### `Symbol.replace`
115114

116-
A regular expression method that replaces matched substrings of a string. Called by the `String.prototype.replace` method.
115+
一个正则表达式方法,用于替换字符串中匹配的子字符串。由 `String.prototype.replace` 方法调用。
117116

118117
### `Symbol.search`
119118

120-
A regular expression method that returns the index within a string that matches the regular expression. Called by the `String.prototype.search` method.
119+
一个正则表达式方法,返回字符串中与正则表达式匹配的索引。由 `String.prototype.search` 方法调用。
121120

122121
### `Symbol.species`
123122

124-
A function valued property that is the constructor function that is used to create derived objects.
123+
一个函数值属性,表示用于创建派生对象的构造函数。
125124

126125
### `Symbol.split`
127126

128-
A regular expression method that splits a string at the indices that match the regular expression.
129-
Called by the `String.prototype.split` method.
127+
一个正则表达式方法,根据匹配正则表达式的索引拆分字符串。由 `String.prototype.split` 方法调用。
130128

131129
### `Symbol.toPrimitive`
132130

133-
A method that converts an object to a corresponding primitive value.
134-
Called by the `ToPrimitive` abstract operation.
131+
将对象转换为对应原始值的方法。由 `ToPrimitive` 抽象操作调用。
135132

136133
### `Symbol.toStringTag`
137134

138-
A String value that is used in the creation of the default string description of an object.
139-
Called by the built-in method `Object.prototype.toString`.
135+
用于生成对象默认字符串描述的字符串值。由内置方法 `Object.prototype.toString` 调用。
140136

141137
### `Symbol.unscopables`
142138

143-
An Object whose own property names are property names that are excluded from the 'with' environment bindings of the associated objects.
139+
一个对象,其自身属性名称是与关联对象的 'with' 环境绑定中排除的属性名称。

0 commit comments

Comments
 (0)