Home > Web Front-end > JS Tutorial > body text

Symbols and Objects in JS♥

Linda Hamilton
Release: 2024-10-11 18:38:02
Original
641 people have browsed it

Symbols and Objects in JS♥

Symbol Datatype

Symbol is a datatype in JS. It typically used for creating unique keys and hidden object keys in Javascript.

There are two types of symbols

  1. Local Symbols - They are not registered in global symbol registry and values are unique even with same descriptor.
  2. Global Symbols - They are registered in global symbol registry and values are not unique

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
Copy after login

Key difference

Descriptor of same values are not same in local symbols

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
Copy after login

Descriptor of same values are same in global symbols

const key_one = Symbol.for("foo");
const key_two = Symbol.for("foo");
console.log(key_one === key_two); //true
Copy after login

some facts about objects and symbols

  1. symbols are not converted into string.
  2. You cannot access both symbols using for...in loop
  3. You cannot access both symbols even with Object.keys() property
  4. Every key in objects are converted into string even numbers.
  5. You can see all symbols of an object using Object.getOwnPropertySymbols() function

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)]
Copy after login

how to get descriptor in Symbol.for()

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
Copy after login

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!