Javascript Data Structure Stack via backend array
function Stack(array) { this.array = [];// ww w.j a v a2 s . c o m if (array) this.array = array; } Stack.prototype.getBuffer = function() { return this.array.slice(); } Stack.prototype.isEmpty = function() { return this.array.length == 0; } Stack.prototype.peek = function() { return this.array[this.array.length - 1]; } Stack.prototype.push = function(value) { this.array.push(value); } Stack.prototype.pop = function() { return this.array.pop(); }; //instance of the stack class var stack1 = new Stack(); console.log(stack1); // {array: []} stack1.push(1); stack1.push(2); stack1.push(3); console.log(stack1); // {array: [1,2,3]} stack1.pop(1); stack1.pop(2); stack1.pop(3); console.log(stack1); // {array: []} stack1.push(10); console.log(stack1.peek()); // 10 stack1.push(5); console.log(stack1.peek()); // 5