logo

NJP

Symbols - Let's Learn ECMAScript 2021 with Earl Duque

Import · Jan 25, 2024 · video

Okay, So for this lesson,
we are going to be talking about symbols. So symbols are primitive data types. Think of them like number
or string or boolean. They're unique. They're immutable. So they're great and ideal for things like creating non colliding property keys in objects. One of the main uses of symbols
is to add unique property keys to objects so that they don't interfere or collide
with other property keys. So let me show you how to create them
and how to use them. So I'm going to create mySymbol
as a variable. And to create a symbol,
you just use the built in JavaScript syntax of Symbol
and then pass it a parameter. And I'm going to say
this one is my unique symbol. There, you just created a symbol. So what that looks like when you're
actually using that in objects. So let's say myObject is here and inside of it I can use this special, unique symbol as a property key. And how I do that is I surround it
with close brackets. So my symbol and now I can set that's whatever I want. A value or something. Okay. So if I want to access that property
and that value, it's as simple as providing that symbol again, I'm going to go ahead and run this. And there you go. My symbol value appeared.
why is this important? Because if I try to create another symbol with the same exact property, And add that into my object, mySymbol2 I can run this and it works regardless,
even though I use the same exact text string for both of those symbols
because I set that string as the symbol type no matter what, it still treats
it as a separate symbol no matter what. They will not collide. Symbols also can simulate
private properties inside classes as they are not accessible through
standard object property enumerators. And so say you have a class right here. I have a class called secretHolder,
and inside of it is holding my secret key. and then additionally,
I have a function called reveal secret if it's called directly. I created a new instance of that class
and passed in a parameter called MySecret. And if I try to do a log statement of Holder's secret key to try to find what
that secret key is, or even if I try to brute force
my way through all the properties within this object,
if I run this all them are undefined. So the normal ways of trying to
see all the property names within it are not accessible
through normal enumeration. But if I go ahead and call directly,
that reveals secret function, a function
that's there which is not revealed to the user
that is using the class necessarily. but instead, if I call reveal secret,
then it's intentionally calling the instance. But if instead I call reveal secret? Because the context of this
is still holding on to that symbol within the class. Then I can finally reveal it. It's a little bit complicating, it's helpful to keep that knowledge
in the back of your head that Symbols are one way
to keep class properties private.

View original source

https://www.youtube.com/watch?v=15yGjODJX_k