ES6 - Symbol Introduction
First, what the Symbol are?
Symbol
Is ES6
a 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、Symbol
As 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 toString
method to convert a string.
let symbol1 = Symbol('some');
let symbol2 = Symbol('some'); symbol1 === symbol2;
//false
3, which means that every Symbol
value created is not the same. Note that you can not use new
way 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 ES5
attribute 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 ES6
introduced into the Symbol
root 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 Symbol
type of attribute names. Please note that it is not private property. We can use the Object.getOwnPropertySymbols
method to obtain the specified object Symbol
attributes. It returns the current object of all Symbol
property types in Symbol
an 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.ownKeys
can 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 Symbol
value 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 Symbol
always to produce a new value, and Symbol.for
will first see if the given key
value exists, does not exist to go to New, and will be registered in the global environment for search. We can use Symbol.keyFor
to view a registered Symbol
value of key
:
Symbol.keyFor(a); // "hello"
Reference link: https: //www.jianshu.com/p/971996a7471f