数据结构_集合

//集合,不允许重复,无序
  class Gather {
    constructor () {
      this.items = {}
    }
    add (prop) {
      if (this.isHas(prop)) return false
      else this.items[prop] = undefined
      return true

    }
    remove (prop) {
      if (!this.isHas(prop)) return false
      delete this.items[prop]
      return true
    }
    //判断属性是否存在集合中
    isHas (prop) {
      return this.items.hasOwnProperty(prop)
    }
    clear () {
      this.items = {}
      return true
    }
    size () {
      return Object.keys(this.items).length
    }
    values () {
      return Object.keys(this.items)
    }
  }

猜你喜欢

转载自www.cnblogs.com/JunLan/p/12355888.html