papercats/stack.js
2017-02-22 06:34:59 +00:00

44 lines
694 B
JavaScript

this.Stack = (function()
{
function Stack(initSize)
{
var len = 0;
var arr = [];
this.ensureCapacity = function(size)
{
arr.length = Math.max(arr.length, size || 0);
};
this.push = function(ele)
{
this[len] = ele;
len++;
};
this.pop = function()
{
if (len === 0)
return;
len--;
var tmp = this[len];
this[len] = undefined;
return tmp;
};
this.isEmpty = function() {
return len === 0;
}
this.ensureCapacity(initSize);
Object.defineProperty(this, "length", {
get: function() {return len;}
});
}
return Stack;
})();