js hack解决方案

  1. 兼容map,旧版本ie或者谷歌可能不支持new Map()方法,使用下面的方法。
function initMap(){
        var mapTemp = function () {
            this.array = [];
        }
        mapTemp.prototype.put =mapTemp.prototype.set =  function(key, value) {
            var index = this.getIndex(key);
            if (index > -1) {
                this.array[index] = {key: key, value: value};
            } else {
                this.array.push({key: key, value: value});
            }
        };
        mapTemp.prototype.get = function(key) {
            var index = this.getIndex(key);
            if (index > -1) {
                return this.array[index].value;
            } else {
                return undefined;
            }
        };
        mapTemp.prototype.getAtIndex = function(index) {
            return this.array[index].value;
        };
        mapTemp.prototype.getKeyAtIndex = function(index) {
            return this.array[index].key;
        };
        mapTemp.prototype.remove = function(value) {
            var index = this.getIndexOfValue(value);
            this.removeByIndex(index);
        };
        mapTemp.prototype.removeByKey = function(key) {
            var index = this.getIndex(key);
            this.removeByIndex(index);
        };
        mapTemp.prototype.removeByIndex = function(index) {
            if(index != -1) {
                this.array.splice(index, 1);
            }
        };
        mapTemp.prototype.clear = function() {
            this.array = new Array(0);
        };
        mapTemp.prototype.all = function() {
            return this.array;
        };
        mapTemp.prototype.getAllValues = function() {
            var valuesArray = [];
            for (var i=0; i<this.array.length; i++) {
                valuesArray.push(this.array[i].value);
            }
            return valuesArray;
        };
        mapTemp.prototype.getIndex = function(key) {
            var index = -1;
            for (var i=0; i<this.array.length; i++) {
                var item = this.array[i];
                if (item.key == key) index = i;
            }
            return index;
        };
        mapTemp.prototype.getIndexOfValue = function(value) {
            var index = -1;
            for (var i=0; i<this.array.length; i++) {
                var item = this.array[i];
                if (item.value == value) index = i;
            }
            return index;
        };
        mapTemp.prototype.getKeyOfValue = function(value) {
            var key = undefined;
            for (var i=0; i<this.array.length; i++) {
                var item = this.array[i];
                if (item.value == value) key = item.key;
            }
            return key;
        };
        mapTemp.prototype.contains = function(value) {
            var index = this.getIndexOfValue(value);
            return index > -1;
        };
        mapTemp.prototype.containsKey = function(key) {
            var index = this.getIndex(key);
            return index > -1;
        };
        mapTemp.prototype.size = function() {
            return this.array.length;
        };
        window.Map = mapTemp; //将map 挂载到window下。
    }

定义好上面的initMap之后,使用

function mapHack() {
        try{
            new Map();
        }catch(e){
            initMap();
        }
    }

2.兼容ArrayBuffer.prototype.slice方法

function arrayPrototypeSlice() {
        if (!ArrayBuffer.prototype.slice)
            ArrayBuffer.prototype.slice = function (start, end) {
                var that = new Uint8Array(this);
                if (end == undefined) end = that.length;
                var result = new ArrayBuffer(end - start);
                var resultArray = new Uint8Array(result);
                for (var i = 0; i < resultArray.length; i++)
                    resultArray[i] = that[i + start];
                return result;
            }
    }

转载于:https://my.oschina.net/u/1267791/blog/824056

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值