Skip to content

Instantly share code, notes, and snippets.

@diegolameira
Last active January 3, 2017 15:38
Show Gist options
  • Save diegolameira/79762aefc3e63a773778 to your computer and use it in GitHub Desktop.
Save diegolameira/79762aefc3e63a773778 to your computer and use it in GitHub Desktop.
Just a wrapper Class for LIFO, obviously Array.pop do the job standalone.
var Stack = (function(){
'use strict';
// Class private shared vars
var place = 'kitchen';
// Constructor
function Stack(some){
// Instance private vars
var something = 'An instance private variable ' + some;
console.log('A new instance of Stack available', something);
// Instance public vars
this.items = new Array();
}
Object.defineProperties(Stack.prototype, {
'place': {
enumerable: false,
configurable: false,
get: function(){
return place;
},
set: function(data){
place = data;
}
},
'length': {
enumerable: false,
configurable: false,
get: function(){
return this.items.length
}
}
})
Stack.prototype.add = function(item){
return this.items.push(item);
}
Stack.prototype.get = function(idx){
return !idx ? this.items.pop() : this.remove(idx);
}
Stack.prototype.list = function(){
return this.items;
}
Stack.prototype.remove = function(idx){
return this.items.splice(idx, 1);
}
return Stack;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment