Note * A Hash table is a data structure that accesses a memory location based on a Key value. From the Wiki

Hash maps are often used in JavaScript as a simple place to store key-value pairs. However, Object is not a true hash map and can be potentially problematic if used incorrectly. And while JavaScript may not provide native hash mapping (at least not cross-browser compatible), there is a better way to declare object properties.

The problem with declaring members in Object

This problem may be due to the inheritance mechanism of the object prototype chain. In the case of the toString method, if we use the in operator to determine whether an object exists:

var map = {};

'toString' in map; // true

The in operator looks through all prototypes to see if the object exists. To solve this problem, use the hasOwnProperty method to check if the object exists:

var map = {};

map.hasOwnProperty('toString'); // false

This method works fine, but if you define a hasOwnProperty property it can be troublesome:

var map = {};

map.hasOwnProperty = 'foo';

map.hasOwnProperty('hasOwnproperty'); // TypeError

A quick fix for this is to use the native object approach.

var map = {};

map.hasOwnProperty = 'foo';

{}.hasOwnProperty.call(map, 'hasOwnproperty'); // true

This method doesn’t cause any problems, filtering out methods in the prototype chain every time you determine whether an attribute exists in the object:

var map = {};

var has = {}.hasOwnProperty;

for(var key in map){

    if(has.call(map, key)){

        // do something

Naked objects

The trick to creating a real Hash Map is to uncouple all the prototype objects. We can do this with Object.create

var obj = {};

// is equivalent to:

var obj = Object.create(Object.prototype);

In addition, this approach lets you abandon stereotypes entirely and use NULL inheritance directly.

var map = Object.create(null);

map instanceof Object; // false

Object.prototype.isPrototypeOf(map); // false

Object.getPrototypeOf(map); // null

These bare objects (or dictionaries) are ideal for Hasp maps. Because there won’t be any conflicts, it will resist any type conversions, such as this will cause errors.

var map = Object.create(null);

map + ""; // TypeError: Cannot convert object to primitive value

There are no reserved words here, it’s designed for Hash maps, for example.

var map = Object.create(null);

'toString' in map; // false

Further, for… So the in loop is a little bit easier, and we just have to write the loop like this.

var map = Object.create(null);

for(var key in map){

    // do something

Despite these differences, it is no different from regular Object key-value stores. The object can be serialized, prototyped and inherited, as can the use of context variables.

var map = Object.create(null);

Object.defineProperties(map, {

    'foo': {

        value: 1,

        enumerable: true

    'bar': {

        value: 2,

        enumerable: false

});

map.foo; / / 1

map['bar']; / / 2

JSON.stringify(map); // {"foo":1}

{}.hasOwnProperty.call(map, 'foo'); // true

{}.propertyIsEnumerable.call(map, 'bar'); // false

Even the variable detection methods mentioned above are applicable.

var map = Object.create(null);

typeof map; // object

{}.toString.call(map); // [object Object]

{}.valueOf.call(map); // Object {}