Implementing Stacks & Queues
Why might you choose this object-based Stack implementation over an array-based one?
class Stack {
constructor() {
this.size = 0;
this.storage = {};
}
push(element) {
this.storage[this.size] = element;
this.size++;
}
pop() {
if (this.size === 0) return undefined;
this.size--;
const result = this.storage[this.size];
delete this.storage[this.size];
return result;
}
}