This is a simple queue structure, but it demostrates 2 things, private data in a class, and having both a construtctor and a factory function that do the exact same thing
function Queue(){
return createQueue();
}
function createQueue() {
const queue = [];
const q = Object.create(Queue.prototype, {
constructor: {
configurable: true,
enumerable: false,
value: Queue,
writable: true,
}
});
const methods = {
enqueue(item){ return queue.push(item); },
dequeue() { return queue.shift() },
peek() { return queue[0] },
debug() { return queue }
}
Object.assign(q, methods);
Object.defineProperty(q, 'length', { get(){ return queue.length } })
return Object.create(q);
}