Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 29 additions & 33 deletions docs/documentation/zh/reference/Symbols.md
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
---
title: Symbols
title: 符号
layout: docs
permalink: /zh/docs/handbook/symbols.html
oneline: Using the JavaScript Symbol primitive in TypeScript
oneline: 在 TypeScript 中使用 JavaScript Symbol 原始数据类型
translatable: true
---

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

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

```ts
let sym1 = Symbol();

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

Symbols are immutable, and unique.
符号是不可变且唯一的。

```ts
let sym2 = Symbol("key");
let sym3 = Symbol("key");

sym2 === sym3; // false, symbols are unique
sym2 === sym3; // false,符号是唯一的
```

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

```ts
const sym = Symbol();
Expand All @@ -37,7 +37,7 @@ let obj = {
console.log(obj[sym]); // "value"
```

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

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

## `unique symbol`

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.
为了将符号视为唯一字面量,TypeScript 提供了一种特殊类型 `unique symbol``unique symbol` `symbol` 的子类型,仅通过调用 `Symbol()` `Symbol.for()`,或通过显式类型注解生成。此类型只能用于 `const` 声明和 `readonly static` 属性。要引用特定的唯一符号,必须使用 `typeof` 操作符。每个对唯一符号的引用都意味着与给定声明关联的完全独特的身份。

```ts twoslash
// @errors: 1332
declare const sym1: unique symbol;

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

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

// Also works.
// 也正常工作。
class C {
static readonly StaticSymbol: unique symbol = Symbol();
}
```

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

```ts twoslash
// @errors: 2367
Expand All @@ -84,60 +84,56 @@ if (sym2 === sym3) {
}
```

## Well-known Symbols
## 内置符号

In addition to user-defined symbols, there are well-known built-in symbols.
Built-in symbols are used to represent internal language behaviors.
除了用户自定义的符号外,JavaScript 还提供了一些内置的著名符号。这些符号用于表示语言的内部行为。

Here is a list of well-known symbols:
以下是著名符号的列表:

### `Symbol.asyncIterator`

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

### `Symbol.hasInstance`

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

### `Symbol.isConcatSpreadable`

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

### `Symbol.iterator`

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

### `Symbol.match`

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

### `Symbol.replace`

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

### `Symbol.search`

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

### `Symbol.species`

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

### `Symbol.split`

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

### `Symbol.toPrimitive`

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

### `Symbol.toStringTag`

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

### `Symbol.unscopables`

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