#Understanding Symbol primitive data type?

37 messages · Page 1 of 1 (latest)

sonic apex
#

What is the primitive type Symbol is? I don't really understand it, as it seems not intuitive. I know about string, number and boolean primitive types yet Symbol seem to be alien. We can even pass in a string to a Symbol, which is weird as I don't see any other primitives that can do that.

Moreover, I can see the use cases with other primitives, like number can do arithmatic operations and so on. Its not clear what Symbol has of use case.

function printPerson(a: number, b: string) {
    ...
}

Lastly, we can also use other primitives easily in other data structures:

let arr = [1,2,3]
let obj = { name: 'Peter', age: 30 }

But how do we use Symbol? :D

solemn vortex
#

have you gone over the docs page?

#
#

We can even pass in a string to a Symbol, which is weird as I don't see any other primitives that can do that.
wdym by this? passing primitives to the other primitives' constructors is pretty common as a conversion method, like Number("1")

#

note, just like the other primitives, the primitive symbol type is lowercase, as Symbol refers to the object wrapper type

sonic apex
#

But aren't all keys in objects unique anyways?

solemn vortex
#

"unique" here means something that won't be overridden

#

symbols are effectively singletons

sonic apex
solemn vortex
#

"foo" doesn't really become the symbol

sonic apex
#

That way we cast the string to symbol

solemn vortex
#

"foo" is converted into a unique symbol

solemn vortex
sonic apex
#

Number("1") => converts string "1" to number type with value 1 (a number I can do math with and use as values, plus insert into equality logical checks)

Symbol("foo") => converts string "foo" to a symbol. This means its a unique entity, and will not be equal to other "foo" even if we also create another Symbol("foo").

#

But we can't use string methods on the new symbol "foo"

#

Its useful for making unique object keys

#

and we can somehow use it in a way that ensure its not being processed in loops

solemn vortex
#

you can't use string methods on the 1 you get from Number("1") either

solemn vortex
#

imagine this kind of thing though

interface Person {
  children: number;
}

interface DataNode {
  children: DataNode[];
}
```2 keys with the same name that both make sense, but they have different meanings, and so they're incompatible
#

or even if Person.children was a Person[], now you have some uncomfortable overlap

#

an object that wants to implement both Person and DataNode wouldn't have a good time with that

#

symbols let Person specify keys that will never collide with DataNode's keys

sonic apex
# solemn vortex you can't use string methods on the `1` you get from `Number("1")` either

Yep, but for my dumb brain its clear that there is a sort of conversion in this case.

Perhaps a bad example, but this is sort of how I think:
In real life a person says (string) I have a "1" coin variable somewhere at home, the person could also just have said something else like I have a ("1" + "2" = "12") "12" coin variable at home.

Now we want to get that coin and perform real math on it. So we "convert" (go home and find the actual coin with the number 1 and do Number("1") on it), and thus we can add it to our purse and we have more coins (1 + 2 = 3). Now we have 3 coins.

But with symbols, I am trying to understand what it practically means instead of abstractness to make it somehow easier in my head (mental model).

The person now says I have "1" coin variable at home. We want to convert it to a symbol that represent EXACTLY that coin at home, Symbol("1"), and not any other "1" coin variables. Moreover, just adding it to your purse doesn't add it to the other coins you have, since its unique and a singleton.

solemn vortex
#

Yep, but for my dumb brain its clear that there is a sort of conversion in this case.
maybe try thinking of symbols as how it's named: a symbol
you can't really modify or do logic with symbols, they just are

#

Symbol("...") creates a new symbol whose value is unrelated to whatever string is inside, but the string is used to represent the symbol (but it itself is not the symbol)

#

the string just gives a label, it's unrelated to the value

sonic apex
#

Aha, its sort of a label we put on a symbol. It doesn't really do anything, except as a label.

#

I think my pitfall is abstract things like this

#

need to somehow get better at it :)

gaunt verge
#

If you are confused about the use cases of symbols, symbols have two notable properties:

  • No way to obtain a symbol.
  • No way to enumerable symbol properties on an object.
#

These two properties can be used together to control access:

const key = Symbol()

export const obj = {
    [key]: 'my secret value',
}

Even though obj is exported, other code has no way to access key (because key is not exported and you can't enumerate symbol properties) so no one but you can access the secret value.

sonic apex
#
const print = Symbol('print')

const user = {
  name: 'Stefan',
  age: 40,
  [print]: function() {
    console.log(`${this.name} is ${this.age} years old`)
  }
}
JSON.stringify(user) // { name: 'Stefan', age: 40 }
user[print]() // Stefan is 40 years old
#

This is a great example of Symbol

solemn vortex