JSFiddle - React, Tailwind, and code Playground
JavaScript
function Stack() {
const items = [];
return {
push(item) {
items.push(item);
},
pop() {
return items.pop();
}
}
}
const stack = Stack();
stack.push(3)
stack.push(2)
stack.push(1)
console.log(stack.pop()); // logs 1
stack.length = 0; // Does nothing!
console.log(stack.pop()); // logs 2 (works!)