当前位置:网站首页>ES6 symbol explanation

ES6 symbol explanation

2022-06-09 03:42:00 zhulin1028

ES6 A new type of raw data is introduced Symbol, Represents a unique value .

ES5 Object property names are strings, which easily cause property name conflicts .

var a = { name: 'lucy'};

a.name = 'lili';
 This will override the properties 

ES6 A new type of raw data is introduced Symbol, Represents a unique value .

Review new knowledge : The basic data types are 6 Kind of :UndefinedNull、 Boolean value (Boolean)、 character string (String)、 The number (Number)、 object (Object).

Here a new :Symbol

Be careful ,Symbol Cannot use before function new command , Otherwise, an error will be reported . This is because of the creation of Symbol Is a primitive type of value , Not object

Symbol Function can take a string as an argument , Said to Symbol Description of the example , Mainly for display on the console , Or to string , Easy to distinguish .

//  Without parameters 
var s1 = Symbol();
var s2 = Symbol();
s1 === s2 // false
//  With parameters 
var s1 = Symbol("foo");
var s2 = Symbol("foo");
s1 === s2 // false

Symbol Value cannot be evaluated with other types of values

As the property name Symbol

var mySymbol = Symbol();
//  The first way to write it 
var a = {};
a[mySymbol] = 'Hello!';
//  The second way 
var a = {
  [mySymbol]: 'Hello!'
};
//  The third way 
var a = {};
Object.defineProperty(a, mySymbol, { value: 'Hello!' });
//  All of the above results are the same 
a[mySymbol] // "Hello!"

Be careful ,Symbol Value as object property name , You can't use the dot operator .

var a = {};
var name = Symbol();
a.name = 'lili';
a[name] = 'lucy';
console.log(a.name,a[name]);//lili,lucy

Symbol Value as property name , This property is also a public property , It's not a private property .

This is a bit like java Medium protected attribute (protected and private The difference between : It is inaccessible outside the class , Subclasses within a class can inherit protected Cannot inherit private

But here Symbol It is also accessible outside the class , It just won't show up in for...infor...of In circulation , And not by Object.keys()Object.getOwnPropertyNames() return . But there's one Object.getOwnPropertySymbols Method , You can get all of the Symbol Property name

Symbol.for(),Symbol.keyFor()

Symbol.for The mechanism is somewhat similar to the singleton pattern , First, search the global for any with this parameter as the name Symbol value , If there is , I'm just going to return this Symbol value , Otherwise, it will create and return a string named Symbol value . And direct Symbol It's a little different .

var s1 = Symbol.for('foo');
var s2 = Symbol.for('foo');
s1 === s2 // true

Symbol.keyFor Method returns a registered Symbol The type is worth key. The essence is to detect the Symbol Created or not

var s1 = Symbol.for("foo");
Symbol.keyFor(s1) // "foo"
var s2 = Symbol("foo");
Symbol.keyFor(s2) // undefined

原网站

版权声明
本文为[zhulin1028]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/160/202206090335352799.html