完整可运行代码classStack{constructor(){this.items=[]}push(element){this.items.push(element)}pop(){returnthis.items.pop()}peek(){returnthis.items[this.items.length-1]}isEmpty(){returnthis.items.length===0}size(){returnthis.items.length}clear(){this.items=[]}toString(){letresultString=''for(constitemofthis.items){resultString+=item+''}returnresultString}}//letstack=newStack()stack.push(1)stack.push(2)stack.push(3)console.log(stack.items)//-->[1,2,3]console.log(stack.pop())//-->3console.log(stack.peek())//-->2console.log(stack.isEmpty())//-->falseconsole.log(stack.size())//-->2console。日志(堆栈。toString());//-->12
