Home > Mobile >  How to use built-in Map in custom Map in javascript
How to use built-in Map in custom Map in javascript

Time:01-29

I have a class named Map, and I want to define a member variables as built-in Map.
Here is my code.

class Map {

constructor() {

this.myMap = new Map();

}

}

It seems like system will recognize it as a recursive call.

I can use object instead of built-in Map, but I want to use float number as the key.

CodePudding user response:

Refer to window.Map to get the global Map instead of this particular class.

class Map {
    constructor() {
        this.myMap = new window.Map();
    }
}
const m = new Map();
m.myMap.set('foo', 'bar');
console.log('done');

  • Related