• 重写默认方法兼容ie, 比如filter,keys等


    重写 Object.keys, Array.prototype.map, Array.prototype.filter, Array.prototype.reduce, 使其适合ie

    1. keys

    Object.myKeys = function (params) {
        var ary = [];
        if (Object.keys) {
            ary = this.keys(params);
        } else {
            for(var key in params ) if(hasOwn.call(params,key)){
                ary.push(key) ;
            }
            var DONT_ENUM =  "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(","),
                hasOwn = ({}).hasOwnProperty;
            for (var i in {
                toString: 1
            }){
                DONT_ENUM = false;
            }
            if(DONT_ENUM && params){
                for(var i = 0 ;key = DONT_ENUM[i++]; ){
                    if(hasOwn.call(params,key)){
                        ary.push(key);
                    }
                }
            }
        }
        return ary;
    }

    2.使ie 支持map

    Array.prototype.myMap = function (fn, context) {
        context = context || window;
        var ary = [];
        if (Array.prototype.map) {
            ary = this.map(fn, context);
        } else {
            for (var i = 0; i < this.length; i++) {
                ary[i] = fn.apply(context, [this[i], i, this]);
            }
        }
        return ary;
    }

    3. filter

    Array.prototype.myFilter = function (fn) {
        var ary = [];
        if (Array.prototype.filter) {
            ary = this.filter(fn);
        } else {
            if (this === void 0 || this === null) {
                throw new TypeError();
            }
            var t = Object(this);
            var len = t.length >>> 0;
            if (typeof fn !== "function")
              throw new TypeError();
    
            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 (fn.call(thisArg, val, i, t))
                  ary.push(val);
              }
            }
        }
        return ary;
    }

    4. refuce

    Array.prototype.myReduce = function (callback, opt_initialValue) {
        var value = '';
        if (Array.prototype.reduce) {
            value = this.reduce(callback, opt_initialValue);
        } else {
            if (this === void 0 || this === null) {
              throw new TypeError();
            }
            var index,
                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) {
                value = '';
                //throw new TypeError('Reduce of empty array with no initial value');
            }
        }
        return value;
    }
  • 相关阅读:
    Source not found for GeneratedMethodAccessor127.invoke(Object, Object[]) line: not available
    Common Lisp学习资源整理
    安装lispbox出现:error while loading shared libraries: libjpeg.so.62: cannot open shared object file: No such file or directory
    《完全用Linux工作》作者:王垠
    记一次奇怪的电脑故障
    Linux下修改键盘映射
    百度与谷歌的差别,英文检索的重要性
    你今天的习惯决定了你将来的位置
    Coursera上一个不错的Java课
    无限制使用ppt转pdf功能
  • 原文地址:https://www.cnblogs.com/floraCnblogs/p/compatible_ie.html
Copyright © 2020-2023  润新知