Symbol Type Flashcards

1
Q

By specification, only two primitive types may serve as object property keys:

A

string type,

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

A _______ represents a unique identifier.

A

“symbol”

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

let id1 = Symbol(“id”);
let id2 = Symbol(“id”);

alert(id1 == id2); // Returns and why?

A

false

Symbols are guaranteed to be unique.

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

let id1 = {};
let id2 = {};

alert(id1 == id2); returns

A

false

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

let id1 = {};
let id2 = id1;

alert(id1 == id2); returns?

A

true

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

Symbols do or don’t auto-convert to a string

A

don’t

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

let id = Symbol(“id”);
alert(id); // returns?

A

TypeError: Cannot convert a Symbol value to a string

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

let id = Symbol(“id”);
alert(id.toString()); // returns

A

Symbol(id), now it works

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

let user = { // belongs to another code
name: “John”
};

let id = Symbol(“id”);

// assign id to user object

alert( user[id] ); // we can access the data using the symbol as the key

A

user[id] = 1;

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

If we want to use a symbol in an object literal {…}

A

we need square brackets around it.

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

let id = Symbol(“id”);

let user = {
name: “John”,
// code here
};

what is correct [id]: 123 or [“id”]:123 and why?

A

That’s because we need the value from the variable id as the key, not the string “id”.

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

Symbolic properties do not participate in_________ loop

A

for..in loop.

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

let id = Symbol(“id”);
let user = {
name: “John”,
age: 30,
[id]: 123
};

for (let key in user) alert(key); // returns?

A

name, age (no symbols)

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

let id = Symbol(“id”);
let user = {
[id]: 123
};

let clone = Object.assign({}, user);

alert( clone[id] ); // returns

A

123

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

Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry:__________ returns (creates if needed) a global symbol with key as the name

A

Symbol.for(key)

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

let sym = Symbol.for(“name”);
// get name by symbol

alert( ); // name

A

Symbol.keyFor(sym)

17
Q

let sym2 = Symbol.for(“id”);

alert( ); // id

A

Symbol.keyFor(sym2)