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

JavaScript 集合

时间:2016-01-11 22:11:44      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

function Set() {
	var items = {};
	this.has = function(value) {
		return value in items
	}
	this.add = function(value) {
		if (!this.has(value)) {
			items[value] = value;
			return true
		}
		return false
	}
	this.remove = function() {
		if (this.has(value)) {
			delete items[value];
			return true
		}
		return false
	}
	this.size = function() {
		return Object.keys(items).length
	}
	this.values = function() {
		return Object.keys(items)
	}
	this.union = function(otherSet) {
		var unionSet = new Set();
		var values = this.values();
		for (var i = 0; i < values.length; i++) {
			unionSet.add(values[i])
		}
		values = otherSet.values();
		for (var i = 0; i < values.length; i++) {
			unionSet.add(values[i])
		}
		return unionSet
	}
	this.intersection = function(otherSet) {
		var intersection = new Set();
		var values = this.values();
		for (var i = 0; i < values.length; i++) {
			if (otherSet.has(values[i])) {
				intersection.add(values[i])
			}
		}
		return intersection
	}
	this.difference = function(otherSet) {
		var differece = new Set();
		var values = this.values();
		for (var i = 0; i < values.length; i++) {
			if (!otherSet.has(values[i])) {
				differece.add(values[i])
			}
		}
		return differece
	}
	this.subSet = function(otherSet) {
		var subSet = new Set();
		if (this.size() > otherSet.size()) {
			return false
		}
		var values = this.values();
		for (var i = 0; values.length; i++) {
			if (!otherSet.has(values[i])) {
				return false
			}
		}
		return true
	}
}

  

JavaScript 集合

标签:

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

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