首頁 > web前端 > js教程 > 主體

详解JS中Array对象扩展与String对象扩展

WBOY
發布: 2016-06-01 09:54:44
原創
1078 人瀏覽過

直接给大家上array对象扩展代码了,具体代码如下所示:

<code class="language-javascript">/**
* Created by laixiangran on 2016/01/07.
* Array扩展
*/
(function() {
    // 遍历数组
    if (typeof Array.prototype.forEach != "function") {
        Array.prototype.forEach = function(fn, context) {
            for (var i = 0; i = 0; i--) {
                if (i in this && this[i] === item) {
                    return i;
                }
            }
            return - 1;
        };
    }
    // 让数组元素依次调用给定函数,最后返回一个值(从左到右)
    if (typeof Array.prototype.reduce != "function") {
        Array.prototype.reduce = function(callback, initialValue) {
            var previous = initialValue,
            k = 0,
            length = this.length;
            if (typeof initialValue === "undefined") {
                previous = this[0];
                k = 1;
            }
            if (typeof callback === "function") {
                for (k; k  -1; k -= 1) {
                    this.hasOwnProperty(k) && (previous = callback(previous, this[k], k, this));
                }
            }
            return previous;
        };
    }
    // 去掉重复项(唯一性),返回新数组
    if (typeof Array.prototype.uniq != "function") {
        Array.prototype.uniq = function() {
            var arr = [];
            arr[0] = this[0];
            for (var i = 1; i = 0; i--) {
                if (item === this[i]) {
                    this.splice(i, 1);
                }
            }
            return this;
        };
    }
    // 打乱数组顺序
    if (typeof Array.prototype.shuffle != "function") {
        Array.prototype.shuffle = function() {
            var i = this.length;
            while (i) {
                var j = Math.floor(Math.random() * i);
                var t = this[--i];
                this[i] = this[j];
                this[j] = t;
            }
            return this;
        };
    }
    // 求数组的最大值
    if (typeof Array.prototype.max != "function") {
        Array.prototype.max = function() {
            return Math.max.apply({},
            this)
        };
    }
    // 求数组的最小值
    if (typeof Array.prototype.max != "function") {
        Array.prototype.min = function() {
            return Math.min.apply({},
            this)
        };
    }

    // 判断是否为数组
    if (typeof Array.prototype.isArray != "function") {
        Array.prototype.isArray = function() {
            return Object.prototype.toString.apply(this) === "[object Array]";
        };
    }
} ());</code>
登入後複製

 

下面是string对象扩展代码如下所示:

<code class="language-javascript">/**
* Created by laixiangran on 2015/12/12.
* String扩展
*/
(function() {
    // 十六进制颜色值的正则表达式
    var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/;
    // RGB颜色转换为16进制
    if (typeof String.prototype.rgbToHex != "function") {
        String.prototype.rgbToHex = function() {
            var that = this;
            if (/^(rgb|RGB)/.test(that)) {
                var aColor = that.replace(/(?:\(|\)|rgb|RGB)*/g, "").split(",");
                var strHex = "#";
                for (var i = 0; i </code>
登入後複製

 

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!