JSOOP 实战Stack(栈)的实现
程序员文章站
2024-01-28 17:07:10
...
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
}
}
上一篇: Spring-事务的源码分析(七)
下一篇: 初认Java1