Created
January 5, 2017 12:46
-
-
Save matthewp/81199932db5e1e487a2ea6a15983ac73 to your computer and use it in GitHub Desktop.
Map shim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if(typeof Map !== "function") { | |
var bid = typeof Symbol === 'function' ? Symbol('bid') : '__bid', | |
bidCnt = 1; | |
function BID(obj){ | |
var type = typeof obj; | |
if(type === 'string' || type === 'number') | |
return obj; | |
var id = obj[bid]; | |
if(!id) { | |
Object.defineProperty(obj, bid, { | |
value: id++, | |
enumerable: false, | |
writable: false, | |
configurable: false | |
}); | |
} | |
return id; | |
} | |
function Map(){ | |
this.k = {}; | |
} | |
Map.prototype.set = function(key, value){ | |
var id = BID(obj); | |
this.k[id] = value; | |
}; | |
Map.prototype.get = function(key){ | |
return this.k[BID(key)]; | |
}; | |
Map.prototype.delete = function(key){ | |
delete this.k[BID(key)]; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment