从 ECMAScript 2015 开始,symbol 是一种原始数据类型,就像 number 和 string 一样。
🌐 Starting with ECMAScript 2015, symbol is a primitive data type, just like number and string.
symbol 的值是通过调用 Symbol 构造函数创建的。
tslet sym1 = Symbol();let sym2 = Symbol("key"); // optional string key
符号是不可变的,并且是唯一的。
🌐 Symbols are immutable, and unique.
tslet sym2 = Symbol("key");let sym3 = Symbol("key");sym2 === sym3; // false, symbols are unique
就像字符串一样,符号可以用作对象属性的键。
🌐 Just like strings, symbols can be used as keys for object properties.
tsconst sym = Symbol();let obj = {[sym]: "value",};console.log(obj[sym]); // "value"
符号也可以与计算属性声明相结合来声明对象属性和类成员。
🌐 Symbols can also be combined with computed property declarations to declare object properties and class members.
tsconst getClassNameSymbol = Symbol();class C {[getClassNameSymbol]() {return "C";}}let c = new C();let className = c[getClassNameSymbol](); // "C"
unique symbol
为了能够将符号作为唯一字面量处理,有一种特殊类型 unique symbol 可用。unique symbol 是 symbol 的子类型,并且仅通过调用 Symbol() 或 Symbol.for(),或通过显式类型注解生成。此类型仅允许用于 const 声明和 readonly static 属性,并且为了引用特定的唯一符号,必须使用 typeof 运算符。对唯一符号的每次引用都意味着一个与给定声明绑定的完全唯一的标识。
🌐 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.
tsTrydeclare constsym1 : unique symbol;// sym2 can only be a constant reference.letA variable whose type is a 'unique symbol' type must be 'const'.1332A variable whose type is a 'unique symbol' type must be 'const'.: unique symbol = sym2 Symbol ();// Works - refers to a unique symbol, but its identity is tied to 'sym1'.letsym3 : typeofsym1 =sym1 ;// Also works.classC {static readonlyStaticSymbol : unique symbol =Symbol ();}
因为每个 unique symbol 都有完全独立的身份,任何两个 unique symbol 类型都不能相互赋值或比较。
🌐 Because each unique symbol has a completely separate identity, no two unique symbol types are assignable or comparable to each other.
tsTryconstsym2 =Symbol ();constsym3 =Symbol ();if (This comparison appears to be unintentional because the types 'typeof sym2' and 'typeof sym3' have no overlap.2367This comparison appears to be unintentional because the types 'typeof sym2' and 'typeof sym3' have no overlap.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.
以下是知名符号的列表:
🌐 Here is a list of well-known symbols:
Symbol.asyncIterator
返回对象的异步迭代器的方法,与 for await..of 循环兼容。
🌐 A method that returns async iterator for an object, compatible to be used with for await..of loop.
Symbol.hasInstance
一种方法,用于确定构造函数对象是否将某个对象识别为该构造函数的实例之一。由 instanceof 运算符的语义调用。
🌐 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.
Symbol.isConcatSpreadable
一个布尔值,指示对象应通过 Array.prototype.concat 展平为其数组元素。
🌐 A Boolean value indicating that an object should be flattened to its array elements by Array.prototype.concat.
Symbol.iterator
一种返回对象默认迭代器的方法。由 for-of 语句的语义调用。
🌐 A method that returns the default iterator for an object. Called by the semantics of the for-of statement.
Symbol.match
一种正则表达式方法,将正则表达式与字符串进行匹配。由 String.prototype.match 方法调用。
🌐 A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method.
Symbol.replace
一种正则表达式方法,用于替换字符串中匹配的子字符串。由 String.prototype.replace 方法调用。
🌐 A regular expression method that replaces matched substrings of a string. Called by the String.prototype.replace method.
Symbol.search
一个正则表达式方法,返回与正则表达式匹配的字符串中的索引。由 String.prototype.search 方法调用。
🌐 A regular expression method that returns the index within a string that matches the regular expression. Called by the String.prototype.search method.
Symbol.species
一个函数值属性,它是用于创建派生对象的构造函数。
🌐 A function valued property that is the constructor function that is used to create derived objects.
Symbol.split
一种正则表达式方法,用于在与正则表达式匹配的索引处分割字符串。由 String.prototype.split 方法调用。
🌐 A regular expression method that splits a string at the indices that match the regular expression.
Called by the String.prototype.split method.
Symbol.toPrimitive
将对象转换为相应原始值的方法。由 ToPrimitive 抽象操作调用。
🌐 A method that converts an object to a corresponding primitive value.
Called by the ToPrimitive abstract operation.
Symbol.toStringTag
用于创建对象默认字符串描述的字符串值。由内置方法 Object.prototype.toString 调用。
🌐 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.
Symbol.unscopables
一个对象,其自身的属性名称是被排除在相关对象的 ‘with’ 环境绑定之外的属性名称。
🌐 An Object whose own property names are property names that are excluded from the ‘with’ environment bindings of the associated objects.