js增加本地方法

/**
 * @author wsf
 */
/**
 * 为所有对象添加val方法
 */
if (!Object.prototype.val) {
	Object.prototype.val = function() {
		return this.valueOf();
	}
}
/**
 * 判断是否为数字型数据
 */
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);
		}
	}
}
/**
 * 为所有类型数据添加apply方法针对数组和json对象
 */
if (!Object.prototype.applyAll) {
	Object.prototype.applyAll = function(callback) {
		if (!this)
			return;
		var idx = 0;
		if (this.remove && this[0]) {
			//数组
			for (var o = this[0]; idx < this.length && callback.call(o, idx, o) != false; o = this[++idx]) {
			}
		} else {
			//对象
			for (var i in this) {
				if (callback.call(this[i], i, this[i]) === false)
					break;
			}
		}
	}
}
/**
 * 为数组添加indexOf方法
 */
if (!Array.prototype.indexOf) {
	Array.prototype.indexOf = function(o) {
		for (var i = this.length; i >= 0; i--) {
			if (this[i] === o)
				return i;
		}
		return -1;
	}
}
/**
 * 为数组添加contain方法
 */
if (!Array.prototype.contain) {
	Array.prototype.contain = function(o) {
		var validx = this.indexOf(o)//拿到索引
		return validx > 0;
	}
}
/**
 * 为数组添加put方法(添加的到指定下标位置)
 */
if (!Array.prototype.put) {
	Array.prototype.put = function(o, idx) {
		return this.splice(idx, 0, o);
	}
}
/**
 * 为字符对象添加intVal方法
 */
if (!String.prototype.intVal) {
	String.prototype.intVal = function() {
		return parseInt(this);
	}
}
/**
 * 为字符对象添加doubleVal方法
 */
if(!String.prototype.doubleVal){
	String.prototype.doubleVal = function (){
		return parseDouble(this);
	}
}
/**
 * id选择器和类选择器
 */

function selc(s) {
	if (s.indexOf("#") > 0)
		return document.getElementById(s.replace("#", ""));
	else if (s.indexOf(".") > 0) {
		var result = [];
		var a = selector.split(".");
		var prefix = a[0] || "*";
		//前缀
		var suffix = a[1];
		//后缀
		var docs = document.getElementsByTagName(prefix);
		docs.applyAll(function() {
			if (this.nodeType === 1 && this.id) {
				var classNames = this.className.split(/\s+/g);
				//所有类名
				var finded = this;
				classNames.applyAll(function() {
					if (this === suffix)
						result.push(finded);
				});
			}
		});
		return result;
		//返回结果集合
	}
}

/**
 * ajax请求
 * @param {} options
 */
function ajax(options) {
	var httpRequest = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
	httpRequest.onreadystatechange = function() {
		var dataType = options.dataType.toLowerCase();
		httpRequest.readyState === 4 && httpRequest.status === 200 && options.callback(dataType === "json" ? eval("(" + httpRequest.responseText + ")") : dataType === "xml" ? httpRequest.responseXML : httpRequest.responseText, options.context);
	};
	httpRequest.open(options.mode, options.url, options.sync);
	if (options.mode.toLowerCase() === "post") {
		httpRequest.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded;charset=utf-8");
	}
	options.mode.toLowerCase() === "get" ? httpRequest.send(null) : httpRequest.send(options.params);
}

/**
 * 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;
	}
}

 

你可能感兴趣的:(js)