JSOOP 实战Stack(栈)的实现

44 阅读1分钟
const _items = new WeakMap()
class Stack {
  constructor() {
    _items.set(this, [])
  }
  push(obj) {
    _items.get(this).push(obj)
  }
  pop() {
    const items = _items.get(this)
    if (items.length === 0) {
      throw new Error('Stack is empty')
    }
    return items.pop()
  }
  peek() {
    const items = _items.get(this)
    if (items.length === 0) {
      throw new Error('Stack is empty')
    }
    return items[items.length -1]
  }
  get count() {
    return _items.get(this).length
  }
}