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());
    

      

  • 相关阅读:
    c网购物车流程图
    NPOI导Excel样式设置
    一个小时快速搭建微信小程序
    ajax实现过程
    JavaWeb结构
    VS常用快捷键
    MVC+三层架构
    JavaScript跨域总结与解决办法[转]
    HTML5是什么?如何鉴定HTML5产品?[转]
    黑盒测试与白盒测试原理
  • 原文地址:https://www.cnblogs.com/shidengyun/p/5122674.html
Copyright © 2011-2022 走看看