当前位置:网站首页>Concept and function of ES6 symbol
Concept and function of ES6 symbol
2022-06-23 03:01:00 【It workers】
Concept
Symbol Is a data type that provides unique values .
effect
//ES5 We can declare a value more than once var a = 5; var b = 5; a ===b; //true //ES6 The values declared in are never equal , Guarantee uniqueness let a = Symbol(); let b = Symbol(); a===b; //false
If we use it , How to get this value again ?
We can use another declarative approach :
let a = Symbol.for('abc');
let b = Symbol.for('abc');
a===b; //trueSymbol.for Not only does it declare a unique value , You will also check whether the global register has changed the value , If you have already registered , This value will be returned .
Use scenarios
let obj = {
[Symbol.for('abc')]:'111',
abc:'1234'
}If not used Symbol, that key The value repeats , There's another situation , Is someone else inherits your object , It can also cause some problems .
So in this case Symbol, It's perfect , It will not cause a conflict .
How to get objects Symbol Value
notes : for..in.. I can't get it Symbol Of the declared value .
let a = Symbol('a');
let obj = {
[a]:'111',
b:'222',
c:'333'
}
for(let [key,value] of Object.entries(obj)){
console.log('for of',key,value);
}
// Output :
//for of b 222
//for of c 333So how do we take out Symbol The value of ?
Method 1 :Object.getOwnPropertySymbols
let a = Symbol('a');
let obj = {
[a]:'111',
b:'222',
c:'333'
}
Object.getOwnPropertySymbols(obj).forEach(item=>{
console.log(obj[item])
})
// Output :
//111Object.getOwnPropertySymbols The method will only put Symbol Return as an array , Not all key Come back , So is there any way to put all key Come back ?
Method 2 :Reflect.ownKeys
Reflect.ownKeys The method can turn all of key Values are returned .
let a = Symbol('a');
let obj = {
[a]:'111',
b:'222',
c:'333'
}
Reflect.ownKeys(obj).forEach(item=>{
console.log(obj[item]);
})
// Output :
//222
//333
//111边栏推荐
- 2D visual empowerment smart water green intensive development
- Line to line interviewer | one side of ByteDance
- JS judge the mobile terminal and PC terminal
- The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
- What is sitelock? What is the function?
- Storage resources revitalize the system and redefine "hyper fusion"
- Establishment of JMeter distributed pressure measurement environment
- Why can only a small number of condition type prices be maintained in me12 of SAP mm?
- How to use fortress on mobile devices
- Win11 client remote ECS black screen
猜你喜欢

Soft exam information system project manager_ Contract Law_ Copyright_ Implementation Regulations - Senior Information System Project Manager of soft exam 030
What is sitelock? What is the function?

C language series - Section 4 - arrays

Soft exam information system project manager_ Information system comprehensive testing and management - Senior Information System Project Manager of soft test 027

6. template for integer and real number dichotomy

5. concept of ruler method

How to store, manage and view family photos in an orderly manner?

Vulnhub DC-5

8. greed
随机推荐
Aiot application innovation competition -- I am the master of my project, and use gn+ninja to complete the system construction (vscode Development)
Initial xxE
[data preparation and Feature Engineering] perceived data
How to set jewelry label paper
Methods for MySQL to avoid inserting duplicate records
Goframe framework (RK boot): rapid configuration of server CORS
Im web demo invite end hang up error avoidance
The difference between script in head and body
Reading redis source code (II) underlying data structure
How to customize a finished label template
Learning about urldns chains
Quic implementation in rust --- Quinn
PowerShell automated reinstallation of cloudbase init to version 1.1.2
Extract NTDs with volume shadow copy service dit
A penetration of an internal self built shooting range
Exploration on the framework of stream batch integration technology and its practice in kangaroo cloud number stack
The commercial s2b2b e-commerce platform of aquatic industry improves the competitiveness of enterprises and creates a strong engine for industrial development
JS asynchronous mechanism
Precision loss problem
PNAs: power spectrum shows obvious bold resting state time process in white matter