## Maps

var m = {};var x = { id: 1 },    y = { id: 2 };m[x] = "foo";m[y] = "bar";m[x];                            // "bar"m[y];                            // "bar"

var keys = [], vals = [];var x = { id: 1 },    y = { id: 2 };keys.push( x );vals.push( "foo" );keys.push( y );vals.push( "bar" );keys[0] === x;                    // truevals[0];                        // "foo"keys[1] === y;                    // truevals[1];                        // "bar"

var m = new Map();var x = { id: 1 },    y = { id: 2 };m.set( x, "foo" );m.set( y, "bar" );m.get( x );                        // "foo"m.get( y );                        // "bar"

m.set( x, "foo" );m.set( y, "bar" );m.delete( y );

m.set( x, "foo" );m.set( y, "bar" );m.size;                            // 2m.clear();m.size;                            // 0

Map(..)的构造器还可以接受一个可迭代对象（参见第三章的“迭代器”），它必须产生一个数组的列表，每个数组的第一个元素是键，第二元素是值。这种用于迭代的格式与entries()方法产生的格式是一样的，entries()方法将在下一节中讲解。这使得制造一个map的拷贝十分简单：

var m2 = new Map( m.entries() );// 等同于：var m2 = new Map( m );

var x = { id: 1 },    y = { id: 2 };var m = new Map( [    [ x, "foo" ],    [ y, "bar" ]] );m.get( x );                        // "foo"m.get( y );                        // "bar"

### Map 值

var m = new Map();var x = { id: 1 },    y = { id: 2 };m.set( x, "foo" );m.set( y, "bar" );var vals = [ ...m.values() ];vals;                            // ["foo","bar"]Array.from( m.values() );        // ["foo","bar"]

var m = new Map();var x = { id: 1 },    y = { id: 2 };m.set( x, "foo" );m.set( y, "bar" );var vals = [ ...m.entries() ];vals[0][0] === x;                // truevals[0][1];                        // "foo"vals[1][0] === y;                // truevals[1][1];                        // "bar"

### Map 键

var m = new Map();var x = { id: 1 },    y = { id: 2 };m.set( x, "foo" );m.set( y, "bar" );var keys = [ ...m.keys() ];keys[0] === x;                    // truekeys[1] === y;                    // true

var m = new Map();var x = { id: 1 },    y = { id: 2 };m.set( x, "foo" );m.has( x );                        // truem.has( y );                        // false