ES6 - Symbol Introduction

 ES6 - Symbol Introduction

    First, what the Symbol are?

SymbolIs ES6a basic data types newly introduced.

Symbol()Function returns the value of symbol type of the type having a static properties and static methods. It's static properties will be exposed to members of the object of several built-in; it's static methods will expose global symbol registered, and similar to the built-in object class, but as a constructor for it is not complete because it does not support syntax: new Symbol().

    Symbol feature two variables

    1、SymbolAs a new base data type, we can use: a type of variable, it is now unique:let a = Symbol();定义Symbol

let b = Symbol();
a = = = b; //false

    2, it can accept a parameter, we can pass it as a string description, even if the two strings may be identical. If the argument is an object, it will first call the object's toStringmethod to convert a string.

 let symbol1 = Symbol('some');

let symbol2 = Symbol('some'); symbol1 === symbol2; //false

3, which means that every Symbolvalue created is not the same. Note that you can not use newway to create:
let c = new Symbol(); //TypeError: Symbol is not a constructor 

    Third, why should the introduction of Symbol

    1, can increase the property to the object

     In the ES5attribute name before the object of mostly strings, a name is a difficult task, and more than one name at every turn to repeat the mistake. And sometimes we took someone else's code, to give one of the objects increase the property, we do not know how a name is not repeated, which is ES6introduced into the Symbolroot cause.

let name = Symbol('name'); //加入 name 作为描述 let obj = { [name]: 'Baoyuan' //必须放在方括号之中 }; obj[name]; //'Baoyuan' 
    2, traversing the property name

     Object common attributes, we can use Object.keys(),for...in or Object.getOwnPropertyNames()to traverse, but they can not traverse the Symboltype of attribute names. Please note that it is not private property. We can use the Object.getOwnPropertySymbolsmethod to obtain the specified object Symbolattributes. It returns the current object of all Symbolproperty types in Symbolan array of values:

let a = Symbol('a');
let b = Symbol('b'); let c = 'c'; let obj = { [a]: 'a', [b]: 'b', c: 'c' }; let objNames = Object.getOwnPropertySymbols(obj); console.log(objNames); // [Symbol(a), Symbol(b)] let objName = Object.getOwnPropertyNames(obj); console.log(objName); // ["c"] 

     Use Reflect.ownKeyscan traverse all of the attribute names:

let names = Reflect.ownKeys(obj);
console.log(names); // ["c", Symbol(a), Symbol(b)] 
  3, repeated reference value Symbol

   Sometimes a new Symbolvalue in other places we want to use again, this time even if the same descriptors will not have the same value, then we need to Symbol.for():

let a = Symbol.for('hello');
let b = Symbol.for('hello'); a === b; // true 

  In fact, these two will have a new wording Symbol, but the difference is Symbolalways to produce a new value, and Symbol.forwill first see if the given keyvalue exists, does not exist to go to New, and will be registered in the global environment for search. We can use Symbol.keyForto view a registered Symbolvalue of key:

 Symbol.keyFor(a); // "hello"

 

Reference link: https: //www.jianshu.com/p/971996a7471f

Guess you like

Origin www.cnblogs.com/qing-5/p/11434001.html