daqueue
All of the implementations of queues I found attached everything to the
prototype
and didn't make good of private encapsulation, getters,
non-enumerable methods, etc.
This queue does not use Array#shift internally, so it keeps O(1) rather than O(n).
So here it is, a clean implementation of a queue written in straight simple es5.
API
Create a queue
var createQueue = ; var queue = ; // => Creates an empty queue var queue = ; // => Creates a queue with initial values
queue.size
The size
property works exactly the same as length
on Array.
var queue = ; queuesize // => 3
queue.front
A property containing the first item in the queue.
var queue = ; queuefirst // => 'hat'
queue.enqueue()
Add a new item to the queue. Returns the queue for chaining.
var queue = ; queue;queue; // queue is => ['hat', 'tree', 'house']
queue.dequeue()
Gets the next item in the queue.
var queue = ; queue // => 'hat'// queue is => ['tree', 'golf']
queue.toString()
Get the current queue as a string.
var queue = ; queue // => "1,2,3"
queue.toArray()
Copy the current queue into a regular array.
var queue = ; queue // => [1, 2, 3]