function Stack () {
this.list = [];
Stack.prototype.push = (item) => {
this.list.push(item);
}
Stack.prototype.pop = () => {
this.list.pop();
}
Stack.prototype.size = () => {
return this.list.length;
}
Stack.prototype.isEmpty = () => {
return this.list.length === 0;
}
Stack.prototype.peek = () => {
return this.list[this.list.length -1];
}
Stack.prototype.toString = () => {
let result = '';
for ( let i of this.list) {
result = result + i + ' ';
}
return result;
}
}
let stack = new Stack();
console.log('--CL--1--', stack);
stack.push('2222');
stack.push('33');
stack.push('1');
console.log('--CL--2--', stack);
console.log('--CL--3--', stack.size());
stack.pop();
console.log('--CL--4--', stack);
console.log('--CL--5--', stack.peek());
console.log('--CL--6--', stack.isEmpty());
console.log('--CL--7--', stack.toString());
JS数组实现栈