Skip to content

Latest commit

 

History

History
executable file
·
50 lines (36 loc) · 1.41 KB

File metadata and controls

executable file
·
50 lines (36 loc) · 1.41 KB

A note on Symbol

This primitive type is useful for so-called "unique" keys.

Using a symbol, you know no one else who doesn't share this instance (instead of the string) will not be able to set a specific property on a map.

Example without symbols:

var map = {}

function setProp(map) {
  map.prop = "hey"
}
function setProp2(map) {
  map.prop = "hey, version 2"
}

setProp(map)
setProp2(map)

In this case, the 2nd function call will override the value in the first one.

However, with symbols, instead of just using "the string prop", we use the instance itself:

var map = {}
var symbol1 = Symbol("prop")
var symbol2 = Symbol("prop")
// Note in above line, I am creating a symbol with the same key-name, but a different instance of Symbol will be created – so it's a different symbol!

map[symbol1] = 1
map[symbol2] = 2 // doesn't override the previous symbol's value
console.log(map[symbol1] + map[symbol2]) // logs 3

More general notes on Symobls

The Symbol() function returns a value of type symbol, has static properties that expose several members of built-in objects, has static methods that expose the global symbol registry, and resembles a built-in object class, but is incomplete as a constructor because it does not support the syntax "new Symbol()".

var map = {}
var symbol1 = Symbol("prop")
console.log(symbol1) // => Symbol(prop)
Symbol("foo") === Symbol("foo") // false