📜  JavaScript中的symbol

📅  最后修改于: 2020-10-24 08:19:50             🧑  作者: Mango

JavaScript符号

JavaScript Symbol是用于识别对象属性的函数。

要记住的要点

  • Symbol()方法始终返回唯一值。
  • 符号值可以用作对象属性的标识符。
  • 符号是不变的,就像数字或字符串。
  • 不能将符号类型转换为原始数据类型。

句法

Symbol([description])

参量

描述:它是可选的。我们可以将字符串作为参数传递。

JavaScript符号属性

让我们看一下JavaScript Symbol属性的列表及其描述。

Symbol.hasInstance It is used to determine if a constructor object recognizes an object as its instance.
Symbol.isConcatSpreadable It is used to configure if an object should be flattened to its array elements.
Symbol.match It is a method to identify the matching of a regular expression against a string.
Symbol.prototype It is used for produce the prototype for the symbol constructor.
Symbol.replace It replace matched substring of a string.
Symbol.search It returns the index within a string that matches with the regular expression.
Symbol.split It splits a string at the indices that match the regular expression.
Symbol.toPrimitive It is used to convert an object to its equivalent primitive value.
Symbol.unscopables It is a well-known object property whose property name are excluded from with environment.

JavaScript符号方法

让我们看一下JavaScript Symbol Method的列表及其描述。

Symbol.for() It is used to search for existing symbol in a runtime-wide symbol registry with the provided key and returns if it is found. Otherwise new symbol gets created with this key.
Symbol.keyFor() It uses the global symbol registry to look up the key for the symbol. So it doesn’t work for non-global symbols. If the symbol is not global, it won’t be able to find it and returns undefined.
Symbol.toString() It is used to return a string representation of an object.