Simple buffer implementation in JavaScript.

This is my implementation ,to a small project that I had, of an simple buffer to hold some data.


function Buffer(len) {
this.len = len || 10;// the max size of the buffer
this.store = []; // the container to hold data
this.size = 0; // the incremental size of the array
}

Buffer.prototype.add = function(data) {
if(this.store.length < this.len) {
this.store[this.size]=data;
this.size++;
}
};

Buffer.prototype.pop=function() {
var tail = this.size -1;
this.store.splice(tail, 1); // remove
this.size--;
};

Buffer.prototype.dimension = function() {
return this.size;
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s