Data Types Flashcards
How do iterators work in js.
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 }; } } }; };
What are the primitive and non primitive data types?
Primitive: undefined, null, number, bigInt, string, boolean, symbol
Non primitive: object
How to explicitly call an iterator?
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 are iterators different from array-likes
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)
What are the advantages of using a Map over an object?
- Maps can use objects as keys
- Maps retain insertion order
How to create a Map from an Object?
How to create an Object from a Map?
let obj = {
name: “John”,
age: 30
};
let map = new Map(Object.entries(obj));
let obj = Object.fromEntries(map);
alert( map.get(‘name’) ); // John
Compare the map.forEach vs the set.forEach methods
map.forEach((value, key, map) => {})
set.forEach((value, valueAgain, set) => {})
Is there any garbage collection in the following code?
let john = { name: “John” };
let array = [ john ];
john = null;
No. The object is referenced by array[0] so it will not be garbage collected.
How do WeakMaps differ from maps, and when to use them?
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
- 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;