JS中定义一个栈结构

// js中栈的定义
        function Stack() {
            this.top = 0
            this.arr = []
            this.push = push
            this.pop = pop
            this.peek = peek
            this.clear = clear
            this.len = len
            this.isEmpty = isEmpty
            
            function push(ele) {
                this.arr[this.top++] = ele
            }
            function pop() {
                console.log('top:',this.top)
                return this.arr[--this.top]
            }
            function peek() {
                return this.arr[this.top-1]
            }
            
            function clear() {
                delete this.arr
                this.top = 0
                this.arr = []
            }
            function len() {
                return this.top
            }
            function isEmpty() {
                if (this.len() <= 0) {
                    return true
                } else {
                    return false
                }
            }
        }
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容