Data Types Flashcards

1
Q

How do iterators work in js.

A

let range = {
from: 1,
to: 5
};

// 1. call to for..of initially calls this
range[Symbol.iterator] = function() {

// …it returns the iterator object:
// 2. Onward, for..of works only with the iterator object below, asking it for next values
return {
current: this.from,
last: this.to,

// 3. next() is called on each iteration by the for..of loop
next() {
  // 4. it should return the value as an object {done:.., value :...}
  if (this.current <= this.last) {
    return { done: false, value: this.current++ };
  } else {
    return { done: true };
  }
}   }; };
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What are the primitive and non primitive data types?

A

Primitive: undefined, null, number, bigInt, string, boolean, symbol
Non primitive: object

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

How to explicitly call an iterator?

A

let str = “Hello”;

let iterator = strSymbol.iterator;

while (true) {
let result = iterator.next();
if (result.done) break;
alert(result.value); // outputs characters one by one
}

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

How are iterators different from array-likes

A

array-likes are objects that have indexes and a length, while iterators are objects that implement the [Symbol.iterator] method.

let arrayLike = {
0: “Hello”,
1: “World”,
length: 2
};

let arr = Array.from(arrayLike); // (*)
alert(arr.pop()); // World (method works)

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

What are the advantages of using a Map over an object?

A
  1. Maps can use objects as keys
  2. Maps retain insertion order
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create a Map from an Object?
How to create an Object from a Map?

A

let obj = {
name: “John”,
age: 30
};

let map = new Map(Object.entries(obj));
let obj = Object.fromEntries(map);

alert( map.get(‘name’) ); // John

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

Compare the map.forEach vs the set.forEach methods

A

map.forEach((value, key, map) => {})
set.forEach((value, valueAgain, set) => {})

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

Is there any garbage collection in the following code?

let john = { name: “John” };

let array = [ john ];

john = null;

A

No. The object is referenced by array[0] so it will not be garbage collected.

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

How do WeakMaps differ from maps, and when to use them?

A

WeakMaps must have objects as keys.
For weakmaps, when we use an object as the key in it, and there are no other references to the key – the object will be removed from memory (and from the map) automatically.

let john = { name: "John" };

let weakMap = new WeakMap();
weakMap.set(john, "...");

john = null; // overwrite the reference, object is removed from memory!

Use cases:
1. Working with objects from third-party libraries, and we want to store some additional data: When the object dies, we want the additional data to be collected as well!

weakMap.set(john, “secret documents”);// if john dies, secret documents will be destroyed automatically

  1. Caching
let cache = new WeakMap();

// calculate and remember the result
function process(obj) {
  if (!cache.has(obj)) {
    let result = /* calculate the result for */ obj;

    cache.set(obj, result);
    return result;
  }

  return cache.get(obj);
}

// 📁 main.js
let obj = {/* some object */};

let result1 = process(obj);
let result2 = process(obj);

// ...later, when the object is not needed any more:
obj = null;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly