1. <script>
    2. /*
    3. 入栈 push
    4. 出栈 pop
    5. peek 获取栈顶 peek
    6. isEmpty 判断栈是否为空
    7. size 可以看栈中有多少个值
    8. */
    9. function Stack(){
    10. this.items = [];
    11. }
    12. Stack.prototype.push = function(val){
    13. this.items.push(val)
    14. }
    15. Stack.prototype.pop = function(){
    16. var res = this.items.pop()
    17. return res ;
    18. }
    19. Stack.prototype.peek= function(val){
    20. return this.items[this.items.length-1]
    21. }
    22. var s = new Stack();
    23. s.push(2);
    24. s.push(3);
    25. console.log(s.items);[2,3]
    26. console.log(s.pop())//3 出栈
    27. </script>