Recently I reviewed ES6 and found a little understanding of Symbol. Write an article to deepen my impression.

Summary of Symbol

Symbol is an ES6 new primitive data type that represents a unique value. Why did we introduce Symbol? The reason is that the attributes of objects in ES5 are strings, which can easily cause attribute conflicts. To ensure the uniqueness of attribute names and prevent attribute name conflicts, this Symbol was introduced.

The basic use

The Symbol value is generated by the Symbol function. All attribute names that belong to the Symbol type are unique. The only reasonable use of the Symbol type is to store the value of Symbol in a variable and then use the stored value to create object properties. Note that since Symbol is a primitive data type, the Symbol function cannot be preceded by the new command or an error will be reported.

let s = Symbol(); let s1 = Symbol("s"); let s2 = Symbol("s"); let s3 = new Symbol("s") // Symbol is not a constructor console.log(s === s1); //false console.log(s1 === s2); //false let obj = {}; let prop = Symbol(); obj[prop] = 1; console.log(obj[prop]); // 1 console.log(Object.keys(obj)); / / []Copy the code

attribute

ES6 also provides some built-in Symbol values that point to methods used internally in the language. Here are a few. You can check them on MDN.

  • Iterator: The property itself is a function that is generated by the default iterator of the current data structure. Executing this function returns a traverser, which is called for… Of the use of

  • Symbol.hasInstance: This method is called when other objects use the instanceof operator to determine whether they are instances of the object. Example: foo instanceof foo inside the language, the Symbol. HasInstance method inside the property is called.

  • Symbol.toPrimitive: Exists as a function value property of an object, which is called when an object is converted to its original value. Available after ES6 with the highest priority. This involves the concept of implicit conversion, if you are interested in more in-depth understanding.

Here youdao classic subject appeared, example: a = = 1 & & & & a = = a = = 2 3 (rewrite the valueOf/Symbol. ToPrimitive) problem.

const a = { value:0 }
a[Symbol.toPrimitive] = function(){
    return this.value += 1
}
console.log(a==1&&a==2&&a==3) // true
Copy the code

methods

  • Symbol.for(): Sometimes we want to reuse the same Symbol value. Symbol.for() can do this. It takes a string as an argument and searches for a Symbol value with that argument as its name. If so, return the Symbol value, otherwise create a new Symbol with the name of the string and register it globally.
Symbol.for("foo"); // Create a symbol and place it in the symbol registry with the key "foo" symbol.for ("foo"); // Read symbol symbol. for("bar") === symbol. for("bar"); // true, Symbol("bar") === Symbol("bar"); / / false,Copy the code
  • Symbol.kyefor (): Returns the key of a registered Symbol type value. Note: If the query is not registered globally then un______ will be returned.
let s = Symbol('foo')
console.log(Symbol.keyFor(s)) // undefined
Copy the code

The above is my arrangement of Symbol, I hope to be of some help to you.