Symbols Flashcards

1
Q

What is a symbols for? (2 thing)

A

identifiers for unique object properties
Symbols are always unique unless otherwise intended.
So we use a symbol to guarantee we’re creating a unique object key.
Basically so we can make properties that will be used by a different library without clashing keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Symbols show up in the object properties

t or f

A

False

But they’re not private. They’re just not obvious.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Why use symbols?

A

When an object encounters new code, it can be unsafe to have common key names applied.

The symbol as a key protects properties from being altered meeting new code.

let user = { name: "John" };
// Our script uses "id" property
user.id = "Our id value";
// ...Another script also wants "id" for its purposes...
user.id = "Their id value"
// Boom! overwritten by another script!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Create a symbol

A

let id = Symbol()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
What does:
  let id = Symbol()
do?
A

Assigns a unique long number to id.

It’s can then be passed to an object as a unique key that we can access with id

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
What is the argument for?
let id = Symbol("id");
A

It’s a description. It’s for debugging mostly

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
What happens with:
let id1 = Symbol("id");
let id2 = Symbol("id");

alert(id1 == id2);

A

false

The argument is just a description. These are different symbols.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens with:
let id = Symbol(“id”);
alert(id);

A

Error

Symbols don’t auto convert to strings like other data types.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Convert a symbol to a string

A

Symbol.prototype.toString()

returns something like Symbol(‘description’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does:
Symbol.prototype.toString()
do?

A

Returns the symbol like this: Symbol(‘description’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Return a symbol’s description

A

Symbol.prototype.description

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does:
Symbol.prototype.description
do?

A

Return a symbol’s description

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Create a hidden property on an object with a symbol

A
let user = { 
  name: "John"
};

let id = Symbol(“id”);

user[id] = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Retrieve an objects hidden property from a symbol:

let user = { 
  name: "John"
};

let id = Symbol(“id”);

user[id] = 1;

A

user[id] = 1;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Symbols take part in For…In loops

t or f

A

false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Symbols are copied with Object.assign()

t or f

A

True

17
Q

Symbols are returned in Object.keys()

t or f

A

false

18
Q
Create a global symbol:
// the same symbol
alert( id === idAgain ); // true
A
// read from the global registry
let id = Symbol.for("id"); // if the symbol did not exist, it is created
// read it again (maybe from another part of the code)
let idAgain = Symbol.for("id");
// the same symbol
alert( id === idAgain ); // true
19
Q

What does Symbol.prototype.for()

do?

A

Returns a specific global symbol based on the key passed into the argument
Symbol.prototype.for(key)

20
Q

Return a specific global symbol based on the key passed into the argument

A

Symbol.prototype.for(key)

21
Q

Return the symbol key for a given variable name

A

Symbol.keyFor()

let sym = Symbol.for("name");
let sym2 = Symbol.for("id");

// get name by symbol
alert( Symbol.keyFor(sym) ); // name
alert( Symbol.keyFor(sym2) ); // id

22
Q

What does
Symbol.prototype.keyFor()
do?

A

Returns the symbol key for a given variable name

let sym = Symbol.for("name");
let sym2 = Symbol.for("id");

// get name by symbol
alert( Symbol.keyFor(sym) ); // name
alert( Symbol.keyFor(sym2) ); // id

23
Q

What is a system symbol? {incomplete}

A

There exist many “system” symbols that JavaScript uses internally, and we can use them to fine-tune various aspects of our objects.