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
以後,使用this
function mapHack() { try{ new Map(); }catch(e){ initMap(); } }
2.兼容 ArrayBuffer.prototype.slice
方法prototype
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; } }