码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript Dictionary

时间:2016-01-11 23:29:16      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

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

  

JavaScript Dictionary

标签:

原文地址:http://www.cnblogs.com/shidengyun/p/5122674.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!