JS Common functions

/**
 * 获取字符串字符长度(英文为一个字符,中文为两个)
 */
String.prototype.charLen = function() {                 
	return this.replace(/[^\x00-\xff]/g,"rr").length;          
}

/**
 * 从URL中获取传过来的参数
 * @returns json 对象
 */
function getParamsFromUrl(){
    var args = {};
    var match = null;
    var search = decodeURIComponent(location.search.substring(1));
    var reg = /(?:([^&]+)=([^&]+))/g;
    while((match = reg.exec(search))!==null){
        args[match[1]] = match[2];
    }
    return args;
}

/**
 * 判断数组中是否包含某obj
 */
Array.prototype.contains = function(obj) {
	var i = this.length;
	while(i--) {
		if(this[i] === obj) {
			return true;
		}
	}
	return false;
}

/**
 * 从数组中移除一项如果该项存在,返回新的数组
 */
Array.prototype.removeObj = function(obj) {
	if( this.contains(obj) ) {
		var newArray = new Array();
		var arrLen = this.length;
		for(var i = 0; i < arrLen; i ++) {
			if(obj !== this[i]) {
				newArray.push(this[i]);
			}
		}
		return newArray;
	}
	return this;
}


你可能感兴趣的:(JS Common functions)