![]() Symbol.toPrimitiveĪ method converting an object to a primitive value. ![]() Symbol.splitĪ method that splits a string at the indices that match a regular expression. Symbol.speciesĪ constructor function that is used to create derived objects. archĪ method that returns the index within a string that matches the regular expression. Symbol.replaceĪ method that replaces matched substrings of a string. Symbol.matchAllĪ method that returns an iterator, that yields matches of the regular expression against a string. Symbol.matchĪ method that matches against a string, also used to determine if an object may be used as a regular expression. eratorĪ method returning the default iterator for an object. Symbol.isConcatSpreadableĪ Boolean value indicating if an object should be flattened to its array elements. Symbol.hasInstanceĪ method determining if a constructor object recognizes an object as its instance. Symbol.asyncIteratorĪ method that returns the default AsyncIterator for an object. In these Symbols' descriptions, we will use language like " Symbol.hasInstance is a method determining…", but bear in mind that this is referring to the semantic of an object's method having this Symbol as the method name (because well-known Symbols act as "protocols"), not describing the value of the Symbol itself. The static properties are all well-known Symbols. Well-known symbols do not have the concept of garbage collectability, because they come in a fixed set and are unique throughout the lifetime of the program, similar to intrinsic objects such as Array.prototype, so they are also allowed in WeakMap, WeakSet, WeakRef, and FinalizationRegistry objects. This is like the difference between Function.name and "Function". In MDN and other sources, well-known symbol values are stylized by prefixing For example, Symbol.hasInstance is written as This is because symbols don't have actual literal formats, but using Symbol.hasInstance does not reflect the ability of using other aliases to refer to the same symbol. Well-known Symbols allow the customizations to be "invisible" from normal code, which typically only read string properties. However, as more operations are added to the language, designating each operation a "magic property" can break backward compatibility and make the language's behavior harder to reason with. For example, the JSON.stringify function will attempt to call each object's toJSON() method, and the String function will call the object's toString() and valueOf() methods. Prior to well-known Symbols, JavaScript used normal properties to implement certain built-in operations. ![]() ![]() For example, if a constructor function has a method with Symbol.hasInstance as its name, this method will encode its behavior with the instanceof operator. ![]() They are known as well-known Symbols, and their purpose is to serve as "protocols" for certain built-in JavaScript operations, allowing users to customize the language's behavior.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |