Metaprogramming refers to the operation of the target as the program’s own behavior characteristics of the programming, and in ES6 increased the type symbol, in addition to user-defined symbols, but also predefined some other built-in symbols, can be called built-in symbols. Here’s a look at some of these built-in symbols.

  1. Symbol.iterator

This symbol represents a specific property on any object, and the language mechanism automatically looks for a method on that property that constructs an iterator to consume the value of that object. … Expand and the for… The of loop uses it automatically.

We can also define our own iterator logic for any object value by customizing the symbol. iterator property, which overrides the default iterator. We have essentially defined a metaprogramming behavior that the rest of JavaScript (that is, operators and loop constructs) can use when working with defined objects.

  1. Symbol. ToStringTag and Symbol. HasInstance

In a common scenario in everyday logic, toString() and instanceof are commonly used to determine what type a value is.

Now in ES6, you can define the behavior characteristics of these operations. From the above cases, we can see that if the symbol group of the page tour has more intuitive understanding compared with electron, its format can also be referred to as follows:

Swam: page www.pizei.com:

The toStringTag symbol specifies the string value to use when stringing.

The hasInstance symbol is a method on a constructor function that accepts the value of an instance object and returns true or false to indicate whether the value can be considered an instance.

On function. prototype hasInstance default www.pizei.com writable is false and cannot be written. You can bypass it with Object.defineProperty().

  1. Symbol.species

This symbol controls which constructor the class’s built-in methods use when generating new instances. The default behavior of the built-in native constructor symbol. species is return this. If you want to generate a new instance defined, use the new enclosing constructorSymbol. Species, and then inherited classes can according to it to control by which the constructor to create the instance.

  1. Symbol.toPrimitive

Before ES6, when an object had to be cast to a native type for an operation (such as add + or compare ==), you couldn’t control the behavior. Now you can control the behavior through the Symbol. ToPrimitive.

The symbol. toPrimitive method provides a type (type) specifying “string”, “number”, or “default”, depending on the type expected by the operation calling toPrimitive.

The above is the introduction of ES6 Symbol, I hope to help you.