I'm writing a queue in javascript, and am not sure what is the best way to implement it:
Option 1:
Do something like...
var queue = []
...and use push/shift functions to enqueue/dequeue.
Option 2:
Create an object like so:
var myQueue = new function() {
this.queue = [];
this.enqueue(x) = function() { this.queue.push(x) };
this.dequeue() = function { this.queue.shift() };
}
Option 3:
Something else that you suggest?
The only advantage that I can see to using option 2 over 1 is that if I want to create a unique method to call on myQueue (e.g. dequeueTwoElements), I'd be able to do so.
Thoughts?
return this.queue.shift();.