Sets and Maps in JavaScript
FF13 comes out with experimental support for ECMAScript 6 Map and Set objects.
As maps and sets can be easily implemented without this support, they have been introduced as experimental support
It’s important to note :
An Object has a prototype, so there are default keys in the map. However, this can be bypassed using map = Object.create(null).
The keys of an Object are Strings, where they can be any value for a Map. You can get the size of a Map easily while you have to manually keep track of size for an Object.
So far Maps were implemented as normal objects {} and sets were implemented with a simple filter like [].sort.filter( function(v,i,o){return v!==o[i-1];}.
But now FireFox 13 being the first browser to support these two APIs ( Seems more Java like though)
Maps in Javascript with FF13 :
Map operations : map.get(key); map.set(key, value); map.has(key); map.delete(key); map.size();
map = new Map(); Map {}
map.set(“GNU”,“Fun!”);
map.size size()
map.size() 1
map.get(“GNU”) “Fun!”
Sets in Javascript with FF13
Set operations : set.add(value);set.delete(value);set.has(value);set.size();
SAD we still can’t iterate over it! :(
var set = new Set();
undefinedset.has(420) false
set.add(420) undefined
set.has(420) true
set.delete(420) true
set.has(420) false
Important to note!
This is just a prototype implementation of the Set and Map API’s it is (and will) subject to change anytime.
Let’s wait and watch how many will vote for these APIs :)
About Hemanth HM
Hemanth HM is a Sr. Machine Learning Manager at PayPal, Google Developer Expert, TC39 delegate, FOSS advocate, and community leader with a passion for programming, AI, and open-source contributions.