JS: Map Object
New in ES2015.
Map
is the value of the property key "Map"
of the global object.
[see JS: the Global Object]
console.log( window["Map"] === Map ); // true
Type
Map
is a function.
[see JS: Value Types]
// type of Map console.log ( typeof Map === "function" ); // true
Parent
Parent of Map
is Function.prototype
.
[see JS: Prototype and Inheritance]
// parent of Map console.log ( Object.getPrototypeOf ( Map ) === Function.prototype ); // true
Purpose
Purpose of Map
is:
- To create instance of “map” object. Instance of map object is a collection of key/value pairs as a lookup table data structure. This is different from the generic JavaScript object 〔JS: Object Type 〕. The map object instance is designed specifically as a lookup table data structure.
- Used as a namespace to hold general purpose methods for working with “map” instances.
- Holds the property key
"prototype"
. The value ofMap.prototype
is the parent object of all “map” instances.
Facts about map:
- Map instances are collections of key/value pairs.
- The key can be any JavaScript type.
- The value can be any JavaScript type. [see JS: Value Types]
- Keys are always distinct.
- The insertion order of entries are maintained.
[see JS: the Map Object Tutorial]
How Map Determines Uniqueness of Keys
The equality test used for determining whether 2 keys in a map is the same as ===
,
except treatment of NaN
(not a number).
NaN === NaN
return false
, but for map object, NaN
is considered same as any NaN
.
// this is false console.log(NaN === NaN); // false // for map object, NaN is same as any NaN const m = new Map(); m.set(NaN, "n1"); m.set(NaN, "n2"); console.log(m) // Map { NaN => 'n2' }
Constructor
Properties
Reference
ECMAScript 2015 §Keyed Collection#sec-map-objects
Map Topic
Patreon me $5. Ask me question on patreon