JS-字典

function Dictionary() {

//    字典属性

    this.items = {}

//    字典操作方法

//    1.在字典中添加键值对--set方法

    Dictionary.prototype.set = (key,value)=>

{

this.items[key]=value

}

//    2.判断字典中是否有某个key

    Dictionary.prototype.has=(key)=>{

return this.items.hasOwnProperty(key)

}

//    3.从字典中移除元素

    Dictionary.prototype.remove=(key)=>{

//1.判断这个字典中是否有key

        if (!this.has(key))return false

        //2.从字典中删除key

        delete this.items[key]

return true

    }

//    4.根据key获取value

    Dictionary.prototype.get=(key)=>{

return this.has?this.items[key]:undefined

    }

//    5.获取所有的keys

    Dictionary.prototype.keys=()=>{

return Object.keys(this.items)

}

//    6.size方法

    Dictionary.prototype.size=()=>{

return this.keys().length

    }

//    clear方法

    Dictionary.prototype.clear=()=>{

this.items={}

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。