zoukankan      html  css  js  c++  java
  • JavaScript Dictionary

    function Dictionary() {
    	var items = {};
    	this.has = function(key) {
    		return key in items
    	}
    	this.set = function(key, value) {
    		items[key] = value
    	}
    	this.remove = function(key) {
    		if (this.has(key)) {
    			delete items[key];
    			return true
    		}
    		return false
    	}
    	this.get = function(key) {
    		return this.has(key) ? items[key] : undefined
    	}
    	this.values = function() {
    		var values = [];
    		for (var key in items) {
    			if (this.has(key)) {
    				values.push(items[key])
    			}
    		}
    		return values
    	}
    	this.getItems = function() {
    		return items
    	}
    	this.size = function() {
    		return Object.keys(items).length
    	}
    	this.clear = function() {
    		this.items = {}
    	}
    	this.keys = function() {
    		return Object.keys(items)
    	}
    }
    var dictionary = new Dictionary();
    dictionary.set('shidengyun', 'shidengyun@yeah.net');
    dictionary.set('zhujing', 'zhujing@yeah.net');
    console.log(dictionary.has('shidengyun'));
    console.log(dictionary.size());
    console.log(dictionary.keys());
    console.log(dictionary.values());
    console.log(dictionary.get('shidengyun'));
    dictionary.remove('shidengyun');
    console.log(dictionary.keys());
    console.log(dictionary.values());
    console.log(dictionary.getItems());
    

      

  • 相关阅读:
    JSON.stringify() & JSON.parse() 实现
    节流(Throttling) & 防抖(Debouncing)
    instanceof实现
    new实现
    如何实现深拷贝
    #FFF转换为rgba(255,255,255,1)
    hdcms v5.7.0学习笔记
    Laravel5.x 封装的上传图片类
    JQ 封装全选函数
    双击 ajax修改单元格里的值
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5122674.html
Copyright © 2011-2022 走看看