papercats/game-core/stack.js
StevenJoeZhang b535f5c354 Format code
2019-01-16 11:07:52 +08:00

29 lines
529 B
JavaScript

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;
}
});
}
module.exports = Stack;