JavaScript: @@ 符号(两个"at")在ES6 JavaScript中的含义
概述
@@
描述所谓的众所周知的符号(Well-Known Symbols)。(请注意,它实际上不是JS中有效的语法。)
@@name
可以理解为:Symbol.name
参考链接
https://262.ecma-international.org/6.0/#sec-well-known-symbols
6.1.5.1 Well-Known Symbols
Well-known symbols are built-in Symbol values that are explicitly referenced by algorithms of this specification. They are typically used as the keys of properties whose values serve as extension points of a specification algorithm. Unless otherwise specified, well-known symbols values are shared by all Code Realms (8.2).
Within this specification a well-known symbol is referred to by using a notation of the form @@name, where “name” is one of the values listed in Table 1.
翻译
众所周知的符号是本规范的算法明确引用的内置符号值。 它们通常用作属性的键,其值用作规范算法的扩展点。 除非另有说明,众所周知的符号值由所有代码领域 (8.2) 共享。
在本规范中,使用@@name 形式的符号来引用一个众所周知的符号,其中“name”是表 1 中列出的值之一。
Specification Name | [[Description]] | Value and Purpose |
---|---|---|
@@hasInstance | "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. |
@@isConcatSpreadable | "Symbol.isConcatSpreadable" |
A Boolean valued property that if true indicates that an object should be flattened to its array elements by Array.prototype.concat . |
@@iterator | "Symbol.iterator" |
A method that returns the default Iterator for an object. Called by the semantics of the for-of statement. |
@@match | "Symbol.match" |
A regular expression method that matches the regular expression against a string. Called by the String.prototype.match method. |
@@replace | "Symbol.replace" |
A regular expression method that replaces matched substrings of a string. Called by the String.prototype.replace method. |
@@search | "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. |
@@species | "Symbol.species" |
A function valued property that is the constructor function that is used to create derived objects. |
@@split | "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. |
@@toPrimitive | "Symbol.toPrimitive" |
A method that converts an object to a corresponding primitive value. Called by the ToPrimitive abstract operation. |
@@toStringTag | "Symbol.toStringTag" |
A String valued property that is used in the creation of the default string description of an object. Accessed by the built-in method Object.prototype.toString . |
@@unscopables | "Symbol.unscopables" |
An object valued property whose own property names are property names that are excluded from the with environment bindings of the associated object. |