[js][填坑] 几种ES5方法的兼容性封装(polyfill)

#indexOf

            if (!Array.prototype.indexOf) {
                Array.prototype.indexOf = function(searchElement, fromIndex) {
                    var k;
                    if (this == null) {
                        throw new TypeError('"this" is null or not defined');
                    }
                    var O = Object(this);
                    var len = O.length >>> 0;
                    if (len === 0) {
                        return -1;
                    }
                    var n = +fromIndex || 0;
                    if (Math.abs(n) === Infinity) {
                        n = 0;
                    }
                    if (n >= len) {
                        return -1;
                    }
                    k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
                    while (k < len) {
                        if (k in O && O[k] === searchElement) {
                            return k;
                        }
                        k++;
                    }
                    return -1;
                };
            }

#filter

            if (!Array.prototype.filter) {
                Array.prototype.filter = function(fun) {
                    "use strict";
                    if (this === void 0 || this === null)
                        throw new TypeError();
                    var t = Object(this);
                    var len = t.length >>> 0;
                    if (typeof fun !== "function")
                        throw new TypeError();
                    var res = [];
                    var thisArg = arguments.length >= 2 ? arguments[1] : void 0;
                    for (var i = 0; i < len; i++) {
                        if (i in t) {
                            var val = t[i];
                            if (fun.call(thisArg, val, i, t))
                                res.push(val);
                        }
                    }
                    return res;
                };
            }

#reduce

            if ('function' !== typeof Array.prototype.reduce) {
                Array.prototype.reduce = function(callback, opt_initialValue) {
                    'use strict';
                    if (null === this || 'undefined' === typeof this) {
                        throw new TypeError(
                            'Array.prototype.reduce called on null or undefined');
                    }
                    if ('function' !== typeof callback) {
                        throw new TypeError(callback + ' is not a function');
                    }
                    var index, value,
                        length = this.length >>> 0,
                        isValueSet = false;
                    if (1 < arguments.length) {
                        value = opt_initialValue;
                        isValueSet = true;
                    }
                    for (index = 0; length > index; ++index) {
                        if (this.hasOwnProperty(index)) {
                            if (isValueSet) {
                                value = callback(value, this[index], index, this);
                            } else {
                                value = this[index];
                                isValueSet = true;
                            }
                        }
                    }
                    if (!isValueSet) {
                        throw new TypeError('Reduce of empty array with no initial value');
                    }
                    return value;
                };
            }

 #Array.prototype.forEach()

            if (!Array.prototype.forEach) {
                Array.prototype.forEach = function(callback, thisArg) {
                    var T, k;
                    if (this == null) {
                        throw new TypeError(' this is null or not defined');
                    }
                    var O = Object(this);
                    var len = O.length >>> 0;
                    exception.
                    if(typeof callback !== "function") {
                        throw new TypeError(callback + ' is not a function');
                    }
                    if (arguments.length > 1) {
                        T = thisArg;
                    }
                    k = 0;
                    while (k < len) {
                        var kValue;
                        if (k in O) {
                            kValue = O[k];
                            callback.call(T, kValue, k, O);
                        }
                        k++;
                    }
                };
            }

 

#String.prototype.trim()

if (!String.prototype.trim) {
  String.prototype.trim = function () {
    return this.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  };
}

 

转载于:https://www.cnblogs.com/qingmingsang/articles/5311951.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值