Question 33
Question
Answer
class SymbolRegistry {
constructor() {
this.symbols = {}; // Map to store registered symbols
}
register(name) {
if (!this.hasSymbol(name)) {
const symbol = Symbol(name);
this.symbols[name] = symbol;
return symbol;
} else {
console.warn(`Symbol '${name}' already exists in the registry`);
return this.symbols[name];
}
}
hasSymbol(name) {
return !!this.symbols[name];
}
getSymbol(name) {
return this.symbols[name];
}
}
// Usage Example:
const registry = new SymbolRegistry();
const mySymbol1 = registry.register('data');
const mySymbol2 = registry.register('view');
console.log(mySymbol1); // Output unique symbol for 'data'
console.log(registry.getSymbol('data')); // Accessing the registered symbol
Last updated