The Map is a class inspired by the Java Collection Framework. It contains a collection of key / value pairs.
In JavaScript any object can be a collection of properties /
values, so, why a Map?
Basically, having a class allows the Map to contain other utility methods, like
size,
getPairs,
getKeys and
getValues,
as well as other implementations. The Map subclasses are:
Here will be given some examples:
//Basic actions on a Map
var map = new Map();
map.put("1", "One");
map.put("2", "Two");
map.put("3", "Three");
alert(map.size());
alert(map.getKeys());
alert(map.getValues());
map.remove("2");
//Reading the QueryString
var map = new QueryStringMap();
alert(map.getKeys());
//Reading and writing cookies
var map = new CookieMap();
map.put("currentMenu", "file");
alert(map.put("lastLogin"));
//Manipulating object properties
var customer = {name:"John", age:30};
var map = new ObjectMap();
map.put("name", "John Smith");
alert(customer.name) //The name now is John Smith
| Previous: Manipulating events |
Table of Contents | Next: The StringBuffer class |