
Symbol is a datatype in JS. It typically used for creating unique keys and hidden object keys in Javascript.
global symbol registry:The global symbol registry is a space where symbols created using Symbol.for are stored.
syntax
In Below example "john" is a descriptor and key_one and key_two are symbol.
const key_one = Symbol("john"); //local symbol
const key_two = Symbol.for("john"); //global symbol
console.log(typeof key_one) // symbol
console.log(typeof key_two); //symbol
As I told earlier that every symbol is unique even If descriptor is same in local symbols. Lets validate it.
app
const key_one = Symbol("john");
const key_two = Symbol("john");
key_one == key_two // false
key_one === key_two //false
const key_one = Symbol.for("foo");
const key_two = Symbol.for("foo");
console.log(key_one === key_two); //true
app
const zero = Symbol("0");
const temp = {
0:"zero",
1:"one",
[zero]:"zero",
1.1:"one one",
}
const keys = Object.keys(temp); //["0","1","1.1"]
console.log(temp[1.1]) // one one
console.log(Object.getOwnPropertySymbols(temp)) // [Symbol(0)]
Using Symbol.keyFor(symbol) can get you descriptor of global symbols
const key_one = Symbol.for("john")
Symbol.keyFor(key_one) // "john"
typeof Symbol.keyFor(key_one) //string
Please support me on dev.to and linkedin ?. TY?
The above is the detailed content of Symbols and Objects in JS♥. For more information, please follow other related articles on the PHP Chinese website!
Domestic free ChatGPT encyclopedia
Zero-based Java self-study tutorial
python timestamp
Main purpose of file system
What are the new features of Hongmeng 3.0?
The latest price of eth market
How to copy an Excel table to make it the same size as the original
Introduction to common commands of postgresql