Symbols Flashcards

1
Q

What is a symbol in TypeScript and how can you create one?

A

In TypeScript, a symbol is a primitive data type, similar to number and string, introduced in ECMAScript 2015. Symbols are created by calling the Symbol constructor, and they are immutable and unique. They can optionally have a string key.

Here’s an example:

let sym1 = Symbol();
let sym2 = Symbol("key"); 
let sym3 = Symbold("key");

sym2 === sym3 // false, symbols are unique

In this example, sym1 and sym2 are symbols. Even though sym2 and sym3 have the same key, they are not the same symbol.

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

What is the purpose of Symbol.hasInstance?

A

Symbol.hasInstance represents a method that determines if a constructor object recognizes an object as one of the constructor’s instances. It is called by the semantics of the instanceof operator. It allows you to customize the behavior of the instanceof operator.

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

What is the purpose of Symbol.asyncIterator?

A

Symbol.asyncIterator represents a method that returns an async iterator for an object, which can be used with a for await..of loop. This symbol is used to make an object iterable asynchronously.

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

What is a unique symbol in TypeScript and how does it work?

A

A unique symbol in TypeScript is a subtype of symbol that enables treating symbols as unique literals.

To enable treating symbols as unique literals a special type unique symbol is available. unique symbols are produced only from calling Symbol() or Symbol.for(), or from explicit type annotations. This type is only allowed on const declarations and readonly static properties, and in order to reference a specific unique symbol, you’ll have to use the typeof operator. Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.

declare const sym1: unique symbol;
 
// sym2 can only be a constant reference.
let sym2: unique symbol = Symbol();
// Error: A variable whose type is a 'unique symbol' type must be 'const'.
 
// Works - refers to a unique symbol, but its identity is tied to 'sym1'.
let sym3: typeof sym1 = sym1;
 
// Also works.
class C {
  static readonly StaticSymbol: unique symbol = Symbol();
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can symbols be used as keys for object properties in TypeScript?

A

In TypeScript, symbols can be used as keys for object properties. This can be useful when you want to create private properties for an object, as the properties using symbols are not accessible for direct notation.

Here’s an example:

const sym = Symbol();
let obj = {
  [sym]: "value",
};
console.log(obj[sym]); // "value"

In this example, sym is a symbol that is used as a key in the object obj.

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

What is the purpose of Symbol.isConcatSpreadable?

A

Symbol.isConcatSpreadable represents a boolean value indicating that an object should be flattened to its array elements by Array.prototype.concat. This symbol allows you to define if an object should be concatenated with others or if its elements should be concatenated individually.

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

What is the purpose of Symbol.iterator?

A

Symbol.iterator represents a method that returns the default iterator for an object. This symbol is used to make an object iterable, and it’s called by the semantics of the for-of statement.

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

What is the purpose of Symbol.toPrimitive?

A

Symbol.toPrimitive represents a method that converts an object to a corresponding primitive value. This symbol is used when an object is coerced into a primitive type and it’s called by the toPrimitive abstract operation. It allows you to define the behavior of an object when it’s converted to a primitive type.

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

What is the purpose of Symbol.toStringTag?

A

Symbol.toStringTag is a string value that is used in the creation of the default string description of an object. It is called by the built-in method Object.prototype.toString. This symbol allows you to customize the default description of an object.

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

What is the purpose of Symbol.match?

A

Symbol.match represents a regular expression method that matches the regular expression against a string. This symbol is called by the String.prototype.match method, allowing you to define how string matching operations occur.

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

What is the purpose of Symbol.replace?

A

Symbol.replace represents a regular expression method that replaces matched substrings of a string. This symbol is called by the String.prototype.replace method, allowing you to customize the behavior of string replace operations.

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

What is the purpose of Symbol.search?

A

Symbol.search represents a regular expression method that returns the index within a string that matches the regular expression. This symbol is called by the String.prototype.search method, enabling you to define how string search operations behave.

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

What is the purpose of Symbol.species?

A

Symbol.species is a function-valued property that is the constructor function used to create derived objects. This symbol allows you to define what constructor is used when creating derived objects, for example, when calling methods like map, filter or slice which return a new instance of a class.

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

What is the purpose of Symbol.split?

A

Symbol.split represents a regular expression method that splits a string at the indices that match the regular expression. This symbol is called by the String.prototype.split method, allowing you to control how a string is split into substrings.

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

What is the purpose of Symbol.unscopables?

A

Symbol.unscopables represents an Object whose own property names are property names that are excluded from the ‘with’ environment bindings of the associated objects.

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