Map (objects can be keys)
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Map
JavaScript
var Map = function Map() {}
Map.prototype = {
_keys: [],
_values: [],
//is set contains those things:
_strange: {
pz: undefined, //positive Zero
nz: undefined, //negative Zero
nan: undefined //NaN
},
_isStrange: function( key ){
switch( true ) {
case isNaN(key) && typeof key === 'number': return 'nan';
case key === 0 && 1/key === Infinity : return 'pz';
case key === 0 && 1/key === -Infinity : return 'nz';
}
},
set: function(key, value) {
var index, strange = this._isStrange( key );
if( strange ) { console.log(strange )
this._strange[ strange ] = value;
return;
}
console.log(key, value)
index = this._keys.indexOf(key);
if (!~index) {
this._keys.push(key);
this._values.push(value);
} else {
this._values[index] = value;
}
},
get: function(key) {
var index, strange = this._isStrange( key );
if( strange ) {
return this._strange[ strange ];
}
index = this._keys.indexOf(key);
return this._values[index];
},
has: function(key) {
var index, strange = this._isStrange( key );
if( strange ) {
return this._strange[ strange ] !== undefined;
}
index = this._keys.indexOf(key);
return !!~index;
},
'delete': function(key) {
var index, strange = this._isStrange( key );
if( strange ) {
this._strange[ strange ] = undefined;
}
index = this._keys.indexOf(key);
if (~index) {
this._keys.splice(index, 1);
this._values.splice(index, 1);
}
},
}
var map = new Map
var pz =0;
var nz = -0;
map.set(pz , 10);
map.set(nz , 12);
map.set(NaN, 14);
console.log(map.get(pz ), map.has(pz ), map.get(nz ),...