关于IE8不支持Object.keys()的处理

function extend(dst) {
        var h = dst.$$hashKey;


        for (var i = 1, ii = arguments.length; i < ii; i++) {
            var obj = arguments[i];
            if (obj) {
                var keys = Object.keys(obj);
                for (var j = 0, jj = keys.length; j < jj; j++) {
                    var key = keys[j];
                    dst[key] = obj[key];
                }
            }
        }


        setHashKey(dst, h);
        return dst;
    }

Object.keys()是javascript的一个函数,这个函数返回对象的可枚举属性和方法的名称,但在IE8下会报错,在原来代码之前加上一段,即可解决这个错误。修改之后如下:


var DONT_ENUM =  "propertyIsEnumerable,isPrototypeOf,hasOwnProperty,toLocaleString,toString,valueOf,constructor".split(","),
    hasOwn = ({}).hasOwnProperty;
    for (var i in {
        toString: 1
    }){
        DONT_ENUM = false;
    }


    Object.keys = Object.keys || function(obj){//ecma262v5 15.2.3.14
            var result = [];
            for(var key in obj ) if(hasOwn.call(obj,key)){
                result.push(key) ;
            }
            if(DONT_ENUM && obj){
                for(var i = 0 ;key = DONT_ENUM[i++]; ){
                    if(hasOwn.call(obj,key)){
                        result.push(key);
                    }
                }
            }
            return result;
        };


    
    function extend(dst) {
        var h = dst.$$hashKey;


        for (var i = 1, ii = arguments.length; i < ii; i++) {
            var obj = arguments[i];
            if (obj) {
                var keys = Object.keys(obj);
                for (var j = 0, jj = keys.length; j < jj; j++) {
                    var key = keys[j];
                    dst[key] = obj[key];
                }
            }
        }


        setHashKey(dst, h);
        return dst;
    }
————————————————
版权声明:本文为CSDN博主「di8yuansu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/di8yuansu/article/details/45225947

 

posted @ 2022-04-24 21:33  波罗斯の程序日记  阅读(275)  评论(0编辑  收藏  举报