Skip to content

Instantly share code, notes, and snippets.

@matthewp matthewp/map.js
Created Jan 5, 2017

Embed
What would you like to do?
Map shim
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
You can’t perform that action at this time.