• hash的一些方法


    var Hash = function(obj) {
    	obj = obj || {};
    	for (var key in obj) {
    		this[key] = obj[key];	
    	}
    	return this;
    };
    Hash.prototype = {
    	author: "zxx",
    	each: function(fn) {
    		for (var key in this) {
    			this.has(key) && fn(this[key], key, this);	
    		}
    		return this;
    	},
    	has: function(key) {
    		return this.hasOwnProperty(key);
    	},
    	keyOf: function(value) {
    		var keyof = false;
    		for (var key in this) {
    			if (keyof === false && this.has(key) && this[key] === value) {
    				keyof = key;
    			}
    		}
    		return keyof;
    	},
    	hasValue: function(value) {
    		return !!this.keyOf(value);
    	},
    	extend: function(obj) {
    		obj = obj || {};
    		for (var key in obj) {
    			if (obj.hasOwnProperty(key)) {
    				this.set(key, obj[key]);
    			}
    		}
    		return this;
    	},
    	combine: function(obj) {
    		obj = obj || {};
    		for (var key in obj) {
    			if (obj.hasOwnProperty(key)) {
    				this.include(key, obj[key]);
    			}
    		}
    		return this;
    	},
    	erase: function(key) {
    		if (this.has(key)) {
    			delete this[key];
    		}
    		return this;
    	},
    	map: function(fn) {
    		var objMap = {};
    		typeof fn == "function" && this.each(function(value, key) {
    			objMap[key] = fn(value);
    		});
    		return new Hash(objMap);
    	},
    	filter: function(fn) {
    		var objFilter = {};
    		typeof fn == "function" && this.each(function(value, key) {
    			if (fn(value, key) == true) {
    				objFilter[key] = value;
    			}
    		});
    		return new Hash(objFilter);
    	},
    	every: function(fn) {
    		var isEveryPass = true;
    		typeof fn == "function" && this.each(function(value, key) {
    			if (isEveryPass == true && fn(value, key) == false) {
    				isEveryPass = false;
    			}
    		});
    		return isEveryPass;
    	},
    	some: function(fn) {
    		var isSomePass = false;
    		typeof fn == "function" && this.each(function(value, key) {
    			if (isSomePass == false && fn(value, key) == true) {
    				isSomePass = true;
    			}
    		});
    		return isSomePass;
    	},
    	get: function(key) {
    		if (this.has(key)) {
    			return this[key];	
    		}
    		return null;
    	},
    	set: function(key, value) {
    		if (this.has(key) || !this[key]) {
    			this[key] = value;	
    		}
    		return this;
    	},
    	empty: function() {
    		this.each(function(value, key, hash) {
    			delete hash[key];
    		});
    		return this;
    	},
    	include: function(key, value) {
    		if (typeof this[key] + "" === "undefined") {
    			this[key] = value;	
    		}
    		return this;
    	},
    	getClean: function() {
    		var obj = {};
    		for (var key in this) {
    			if (this.has(key)) {
    				obj[key] = this[key];	
    			}
    		}
    		return obj;
    	},
    	getKeys: function() {
    		var arrKeys = [];
    		this.each(function(value, key) {
    			arrKeys.push(key);
    		});
    		return 	arrKeys;
    	},
    	getValues: function() {
    		var arrValues = [];
    		this.each(function(value, key) {
    			arrValues.push(value);
    		});
    		return 	arrValues;
    	},
    	getLength: function() {
    		var length = 0;
    		this.each(function() {
    			length++;
    		});
    		return length;
    	},
    	toQueryString: function() {
    		var queryString = [];
    		this.each(function(value, key) {
    			var queryPart;
    			if (value instanceof Array) {
    				var queryArr = {};
    				for (var from = 0; from<value.length; from++) {
    					queryArr[from] = value[from];	
    				}
    				queryPart = new Hash(queryArr).toQueryString();
    			} else if (typeof value == "object") {
    				queryPart = new Hash(value).toQueryString();
    			} else {
    				queryPart = key + '=' + encodeURIComponent(value);	
    			}
    			
    			if (value !== null) { queryString.push(queryPart); }
    		});
    		
    		return queryString.join('&');
    	}
    };
  • 相关阅读:
    C/C++内存对齐
    Fibonacci
    Count 1 in Binary
    Hash Function
    Fast Power
    Update Bits
    Unique Binary Search Trees
    Java知识体系(持续更新)
    如何发现牛股
    OpenResty究竟解决了什么痛点
  • 原文地址:https://www.cnblogs.com/haohaoday/p/3230175.html
Copyright © 2020-2023  润新知