Symbols - Beau teaches JavaScript

preview_player
Показать описание
Learn how Symbols work in JavaScript ES6!

Code:

More info:

⭐JavaScript Playlists⭐

-
We're busy people who learn to code, then practice by building projects for nonprofits. Learn Full-stack JavaScript, build a portfolio, and get great references with our open source community.

Рекомендации по теме
Комментарии
Автор

I understood the meaning of symbols in javascript, but I am still not sure where to use them in my projects

arazmammadov
Автор

Symbols are new JavaScript primitive, they are very useful when we use it with objects. Object is mostly used as collection in the form of key value pair, but it has a limitation. The key value should always be string, if you provide number, boolean etc then it will convert it into string.(you can use map data structure instead). Symbols are equivalent to objects because their instances are not equal. Symbol is incomplete as constructor because it does not support the syntax "new Symbol()". String passed in symbol(Symbol('foo')) does not have much significance, it is mostly used for debugging purpose.
const people = {};
people.doctor = 'Pranita';
let sym = Symbol();
people[sym] = 'Boo';
"Pranita", Symbol(): "Boo"}
['doctor']
Symbol does not get returned in Object.keys(), this is to maintain backword compatibility.Old code isn't aware of symbol. Reflect.ownKeys() has ability to get all the key values including symbols.

pranitabhagat
Автор

I still don't fully understand lol. Gotta keep trying!

rossthemusicandguitarteacher
Автор

but what is Symbol used for? like number to calculate, string to show text, boolean to check true false ???

danielhat
Автор

I think you can think of symbol as JavaScript attempt at enums.

Like the video says, it always returns unique value optionally associated with some key, that can’t be mistaken with another symbol even with the same key, eg

‘red’ === ‘red’
Symbol(‘red’) !== Symbol(‘red’)

So basically one could use them to be super safe and robust with their enums

const Colors = {
red: Symbol(‘red’),
blue: Symbol(‘blue’)
}

ivanche
Автор

My problem is why use them for enums when static instances of some Color class can be created ?
Someone said you can use them to differentiate objects with otherwise identical data ... But how often does this have to be done and there must be other ways to do it. Multisets can be implemented without them. Maybe they may have some cryptographic use

dankierson
Автор

So would using the symbol() be good for keys in say react lists to create a random key each time?

Biliob
Автор

Sounds similar to UUIDs (universally unique identifier, version 4), no?

PabloBianchiOK
Автор

Yeah, yeah but *what are symbols for ?*

benzflynn
Автор

I use my code to build my evil scientist secret hideout.

LeoPlaw
Автор

Does anyone know what IDE he is using to run the JavaScript

hassanchowdhury
Автор

How can we display unique symbol key in console?

subratopatnaik
Автор

What's the differece between the use of 'let' and the use of 'const' keyword?

glgeorgiou
welcome to shbcf.ru