当前位置:网站首页>Type de symbole
Type de symbole
2022-07-24 02:45:00 【Nancym】
SymbolType
Symbol(Symbole)- Oui.ECMAScript6Nouveau type de données.Le symbole est la valeur originale,Et l'Instance symbolique est unique、Immuable.Le but du symbole est de s'assurer que les propriétés de l'objet utilisent un identificateur unique,Risque de conflit d'attributs
Définition de la DéclarationSymbolDe plusieurs façons
let sym = Symbol() let sym1 = Symbol() console.log(sym == sym1) // false let sym2 = Symbol('Les paramètres de chaîne peuvent être passés comme description du symbole') console.log(sym2.description) // Les paramètres de chaîne peuvent être passés comme description du symbole console.log(sym2) // Symbol(Les paramètres de chaîne peuvent être passés comme description du symbole) console.log(sym2.toString()) // Symbol(Les paramètres de chaîne peuvent être passés comme description du symbole) let sym = Symbol.for('Inscription globale') let sym1 = Symbol.for('Inscription globale') console.log(sym == sym1) // true console.log(Symbol.keyFor(sym)) // Inscription globale console.log(Symbol.keyFor(sym1)) // Inscription globale UtiliserSymbol Résoudre le problème du couplage des chaînes
Dans un objet, si keyNom répété,Derrière.keyVa mettre devantkeyÉcraser,UtiliserSymbol Définition de type valeur unique ,Résoudre le problème de la couverture
let user1 = {
name: 'lisi',
key: Symbol(),
}
let user2 = {
name: 'lisi',
key: Symbol(),
}
let grade = {
[user1.key]: { js: 90, css: 100 },
[user2.key]: { js: 20, css: 40 },
}
console.log(grade) // Symbol(): {js: 90, css: 100}
// Symbol(): {js: 20, css: 40}
console.log(grade[user2.key]) // { js: 20, css: 40 }
Symbol Utilisation dans les tampons
class Cache {
static data = {}
static set(name, value) {
this.data[name] = value
}
static get(name) {
return this.data[name]
}
}
let user = {
name: 'apple',
key: Symbol(),
}
let cart = {
name: 'apple',
key: Symbol(),
}
Cache.set(user.key, user)
Cache.set(cart.key, cart)
console.log(Cache.get(user.key))
Propriétés étendues et protection des propriétés de l'objet
UtiliserSymbolDéfinitionkeyNe sera pasfor..inEtfor..ofTraverser,Adoptionfor ..of Reflect.ownKeys() Qui peut traverser l'objet SymbolPropriétés
let sym = Symbol('C'est unSymbolType')
let hd = {
name: 'doudun',
[sym]: 'doudounnnn',
}
for (const key in hd) {
console.log(key) // name
}
for (const key of Object.getOwnPropertySymbols(hd)) {
console.log(key) // Symbol(C'est unSymbolType)
}
for (const key of Reflect.ownKeys(hd)) {
console.log(key) // name Symbol(C'est unSymbolType)
}
边栏推荐
- O3DE 的Lumberyard 游戏引擎
- X Actual combat - Cloud Server
- Data Lake (XV): spark and iceberg integrate write operations
- Réalisation d'un diagramme de zone de ligne brisée - Diagramme Rose - Diagramme à barres
- Diversity of SIGIR '22 recommendation system papers
- (六)装饰器扩展之[email protected]使用原理
- Chinese scientists have made new progress in high security quantum key distribution networks
- SSM family financial management personal financial management system accounting system
- Make life full of happiness
- Pyg uses messagepassing to build GCN to realize node classification
猜你喜欢

Zone d'entraînement Web d'attaque et de défense (View source, get Post, robots)

Composition API (in setup) watch usage details

IBM: realize the quantum advantage of fault tolerance by 2030

Diversity of SIGIR '22 recommendation system papers

Unity TimeLine使用教程

How to get gait energy map Gei

Dynamic programming-01 knapsack problem
![【补题日记】[2022牛客暑期多校1]C-Grab the Seat](/img/86/1cf3bbc53d9365bb95dae6d532e276.png)
【补题日记】[2022牛客暑期多校1]C-Grab the Seat

攻防世界WEB练习区(backup、cookie、disabled_button)

Unity timeline tutorial
随机推荐
rust allow dead_ code
Liveqing live broadcast on demand streaming media OBS streaming live broadcast how to obtain interface verification token video verification streamtoken and configure token validity
508. The subtree element with the most occurrences and the pure C implementation of hash table method
Tutoriel sur l'utilisation de la ligne de temps unitaire
Unity TimeLine使用教程
[interview: concurrent Article 21: multithreading: activeness] deadlock, livelock, hunger
Understand the low code implementation of microservices
(六)装饰器扩展之[email protected]使用原理
Live800:客户服务无小事,别让服务击溃企业口碑
Interpretation of steam education with the deepening of educational reform
老公,我们现在无家可归了
Reading notes: self cultivation of programmers - Chapter 3
Numberoptional: a tool for converting strings to numbers
No coding is required, and the "asynchronous request reply" mode is automatically implemented
Maximize, minimize, restore, close and move the WinForm form form of C #
js传参时传入 string有数据;传入 number时没有数据;2[0]是对的!number类型数据可以取下标
stb_ Image replaces other libraries
【补题日记】[2022杭电暑期多校1]C-Backpack
【补题日记】[2022牛客暑期多校1]C-Grab the Seat
Wechat applet setting background image does not display problem solution