This is the 10th day of my participation in the August Text Challenge.More challenges in August


preface

Symbol is a basic data type. The Symbol() function returns a value of type Symbol, which has static attributes and static methods. Its static attributes expose several built-in member objects; Its static method exposes the global Symbol registry and is similar to the built-in object class, but as a constructor it is incomplete because it does not support the syntax: “new Symbol()”.

Each Symbol value returned from Symbol() is unique. A symbol value can be used as an identifier for object properties; This is the only purpose of this data type.


const a = Symbol(a);const b = Symbol(15);
const c = Symbol('function');

console.log(typeof a);
// expected output: "symbol"

console.log(b === 15);
// expected output: false

console.log(c.toString());
// expected output: "Symbol(function)"

console.log(Symbol('function') = = =Symbol('function'));
// expected output: false

Copy the code

Symbol.prototype.description

When creating a Symbol, you can add a description.

const sym = Symbol('foo');
Copy the code

In the code above, sym is described as the string foo.

However, reading this description requires converting Symbol explicitly to a string, that is:

const sym = Symbol('foo');

String(sym) // "Symbol(foo)"
sym.toString() // "Symbol(foo)"

Copy the code

ES2019 provides an instance attribute, Description, that returns the description of Symbol directly.

const sym = Symbol('foo');

sym.description // "foo"
Copy the code

Symbol attribute

  • Symbol.length

  • Length property with a value of 0.

Iterative symbols

  • Symbol.iterator: a method that returns the default iterator of an object. befor... ofUse.

  • Symbol.asyncIterator: a method that returns the default asynchronous iterator for an object. befor await ofUse.

Regular expression symbols

  • Symbol.match: a method used to match strings and also to determine whether an object can be used as a regular expression. beString.prototype.match()Use.

  • Symbol.replace: a method to replace substrings that match strings. beString.prototype.replace()Use.

  • Symbol.search: a method that returns the index in a string that matches the regular expression. beString.prototype.search()Use.

  • Symbol.split: : a method of splitting a string at an index that matches a regular expression. beString.prototype.split()Use.

  • Symbol.unscopables: points to an object. This object specifies the usewithKeyword when which attributes will bewithEnvironmental exclusion.
Array.prototype[Symbol.unscopables]
/ / {
// copyWithin: true,
// entries: true,
// fill: true,
// find: true,
// findIndex: true,
// includes: true,
// keys: true
// }

Object.keys(Array.prototype[Symbol.unscopables])
// ['copyWithin', 'entries', 'fill', 'find', 'findIndex', 'includes', 'keys']
Copy the code

As the code above shows, the array has seven attributes that are excluded by the with command.

// When there is no unscopables
class MyClass {
  foo() { return 1; }}var foo = function () { return 2; };

with (MyClass.prototype) {
  foo(); / / 1
}

/ / unscopables
class MyClass {
  foo() { return 1; }
  get [Symbol.unscopables]() {
    return { foo: true}; }}var foo = function () { return 2; };

with (MyClass.prototype) {
  foo(); / / 2
}
Copy the code

The code above makes the with block not look for foo in the current scope by specifying the symbol.unscopables property, meaning that foo will refer to variables in the outer scope.