Dictionary

// ##字典
    //一一对应,key不可以重复,value可以重复,key无序
    // 创建字典的构造函数
    function Dictionary() {
        // 字典属性
        this.items = {}

        // 字典操作方法
        // 在字典中添加键值对
        Dictionary.prototype.set = function (key, value) {
            this.items[key] = value
        }

        // 判断字典中是否有某个key
        Dictionary.prototype.has = function (key) {
            return this.items.hasOwnProperty(key)
        }

        // 从字典中移除元素
        Dictionary.prototype.remove = function (key) {
            // 1.判断字典中是否有这个key
            if (!this.has(key)) return false

            // 2.从字典中删除key
            delete this.items[key]
            return true
        }

        // 根据key去获取value
        Dictionary.prototype.get = function (key) {
            return this.has(key) ? this.items[key] : undefined
        }

        // 获取所有的keys
        Dictionary.prototype.keys = function () {
            return Object.keys(this.items)
        }

        // 获取所有的value
        Dictionary.prototype.values = function () {
            return Object.values(this.items)
        }

        // size方法
        Dictionary.prototype.size = function () {
            return this.keys().length
        }

        // clear方法
        Dictionary.prototype.clear = function () {
            this.items = {}
        }
    }

    // 创建字典对象
    var dict = new Dictionary()

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值