常用的javascript函数

仿java类实现
/**
 * Map实现
 */
var Map = function() {
	this.flag = false;
	this.store = new Array();
	if (!this.flag) {
		Map.prototype.entry = function(key, val) {
			this.key = key;
			this.val = val;
		}
		Map.prototype.put = function(key, val) {
			this.store[this.store.length] = new this.entry(key, val);
		}
		Map.prototype.get = function(key) {
			for (var i = this.store.length - 1; i >= 0; i--) {
				if (this.store[i].key === key)
					return this.store[i].val;
			}
			return null;
		}
		Map.prototype.remove = function(key) {
			for (var i = this.store.length - 1; i >= 0; i--) {
				this.store[i].key === key && this.store.splice(i, 1);
			}
		}
		Map.prototype.keySet = function() {
			var keys = new Array();
			for (var i = 0; i <= this.store.length - 1; i++) {
				keys.push(this.store[i].key);
			}
			return keys;
		}
		Map.prototype.valSet = function() {
			var vals = new Array();
			for (var i = 0; i <= this.store.length - 1; i++) {
				vals.push(this.store[i].val);
			}
			return vals;
		}
		Map.prototype.clear = function() {
			this.store.length = 0;
		}
		Map.prototype.size = function() {
			return this.store.length;
		}
		this.flag = true;
	}
}

/**
 * StringBuilder实现
 */
var StringBuilder = function() {
	this.vStrings = new Array();
	this.flag = false;
	if (!this.flag) {
		StringBuilder.prototype.append = function(str) {
			this.vStrings.push(str);
		}
		StringBuilder.prototype.toString = function() {
			return this.vStrings.join("");
		}
		StringBuilder.prototype.charAt = function(index) {
			return this.toString().charAt(index);
		}
		StringBuilder.prototype.clear = function() {
			this.vStrings.length = 0;
		}
		StringBuilder.prototype.Delete = function(start, end) {
			var tempString = this.toString();
			var prevString = tempString.substring(0, start);
			var nextString = end ? tempString.substring(end) : tempString.substring(start + 1);
			this.clear();
			this.append(prevString);
			this.append(nextString);
		}
		StringBuilder.prototype.length = function() {
			return this.toString().length;
		}
		StringBuilder.prototype.substring = function(start, end) {
			return this.toString().substring(start, end);
		}
		StringBuilder.prototype.replace = function(oldStr, newStr) {
			var newStr = newStr ? newStr : "";
			var tempString = this.toString().replace(new RegExp(oldStr, "g"), newStr);
			this.clear();
			this.append(tempString);
		}
		StringBuilder.prototype.indexOf = function(val) {
			return this.toString().indexOf(val);
		}
		StringBuilder.prototype.lastIndexOf = function(val) {
			return this.toString().lastIndexOf(val);
		}
		StringBuilder.prototype.insert = function(offset, str) {
			var prevString = this.substring(0, offset);
			var middleString = str;
			var nextString = this.substring(offset);
			this.clear();
			this.append(prevString);
			this.append(middleString);
			this.append(nextString);
		}
		StringBuilder.prototype.split = function(sep) {
			return this.toString().split(sep);
		}
		this.flag = true;
	}
}

为类库对象加入新方法
/**
 * @author wsf
 */
 /**
  * 判断是否为数字型数据
  */
 if (!Object.prototype.isNumber) {
	 String.prototype.isNumber = function() {
		 return !this || isNuN(this.val()) ? false : (typeof this.val() === "number");
	 }
 }

 /**
  * 为数组添加remove方法
  */
 if (!Array.prototype.remove) {
	 Array.prototype.remove = function(s) {
		 for (var i = this.length; i >= 0; i--) {
			 if (s === this[i])
				 this.splice(i, 1);
		 }
	 }
 }

/**
 * 为String添加trim方法
 */
if(!String.prototype.trim){
	String.prototype.trim = function (){
		this.replace(/\s*|\s$/g,"");
	}
}

相关推荐