天天看点

js一点简单扩展

//-------------------------------数组扩展-------------------------------

//查找所有匹配的元素

Array.prototype.findAll = function(match) {

    var arr2 = [];

    for (var i = 0, l = this.length; i < l; i++) {

        if (match(this[i])) {

            arr2.push(this[i]);

        }

    }

    return arr2;

};

//查找第一个匹配的元素

Array.prototype.find = function(match) {

    for (var i = 0, l = this.length; i < l; i++) {

        if (match(this[i])) {

            return this[i];

        }

    }

    return null;

};

//判断数组是否包含元素

Array.prototype.contains = function(item) {

    for (var i = 0, l = this.length; i < l; i++) {

        if (this[i] === item) {

            return true;

        }

    }

    return false;

}

//获取元素在数组中的位置

Array.prototype.indexOf = function(item) {

    for (var i = 0, l = this.length; i < l; i++) {

        if (this[i] === item) {

            return i;

        }

    }

    return -1;

}

//简单迭代器

Array.prototype.forEach = function(func) {

    for (var i = 0, l = this.length; i < l; i++) {

        func(this[i]);

    }

}

//数组转换

Array.prototype.convertAll = function(converter) {

    var arr2 = [];

    for (var i = 0, l = this.length; i < l; i++) {

        arr2.push(converter(this[i]));

    }

    return arr2;

}

//移除元素

Array.prototype.remove = function(item) {

    for (var i = this.length - 1; i >= 0; i--) {

        if (this[i] === item) {

            this.splice(i, 1);

        }

    }

}

//在指定位置移除元素

Array.prototype.removeAt = function(index) {

    this.splice(index, 1);

}

//移除所有匹配项

Array.prototype.removeAll = function(macth) {

    for (var i = this.length - 1; i >= 0; i--) {

        if (macth(this[i])) {

            this.splice(i, 1);

        }

    }

}

//清空数组

Array.prototype.clear = function() {

    this.splice(0, this.length);

}

//-------------------------------字符串扩展-------------------------------

//剪切

String.prototype.trim = function() {

    return this.replace(/(^\s*)|(\s*$)/g, arguments[0] || "");

}

//字符串替换函数

String.prototype.replaceAll = function(s1, s2) {

    return this.replace(new RegExp(s1, "gm"), s2);

}

//格式化

String.prototype.format = function() {

    var args = arguments;

    return this.replace(/{(\d{1})}/g, function() {

        return args[arguments[1]];

    });

};

//大量字符串拼接

function StringBuilder() {

    this._strs = [];

}

StringBuilder.prototype.append = function(str) {

    this._strs.push(str);

};

StringBuilder.prototype.toString = function() {

    this._strs.join("");

};

//为 window.location 扩展 params ,其为由Url参数组成的列表

//使用方法:var param1Value = window.location.params["param1Name"];

//其中 param1Name 为当前Url中出现的某一参数名, param1Value 为获取到的该参数名对应的值

(function(loc) {

    var querys = {};

    var search = loc.search;

    if (search.indexOf("?") > -1) {

        search = search.substr(1);

        var arr2 = search.split("&");

        for (var i = 0; i < arr2.length; i++) {

            var index = arr2[i].indexOf("=");

            if (index > -1) {

                var key = arr2[i].substr(0, index);

                var val = arr2[i].substr(index + 1);

                querys[key] = unescape(val);

            }

        }

    }

    loc.params = querys;

})(window.location);