Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a class extending from ES6 Map

Tags:

People also ask

How do you extend a class in JavaScript?

The extends keyword is used to create a child class of another class (parent). The child class inherits all the methods from another class. Inheritance is useful for code reusability: reuse properties and methods of an existing class when you create a new class.

Can you extend a built-in type?

Built-in classes like Array, Map and others are extendable also. Please note a very interesting thing. Built-in methods like filter , map and others – return new objects of exactly the inherited type PowerArray .

Is it possible to extend built-in classes in JS?

Array, Map, and other built-in classes are extendable. The most impressive thing is that built-in methods such as map , filter, and more - return new objects of literally the inherited type NumsArray .


Trying to get away with custom get/set functionality on ES6 Maps. Currently using Babel to transpile my code to ES5.

Chrome Version 41.0.2272.101 m

class MyMap extends Map {
    get(key) {
        if (!this.has(key)) { throw new Error(...); }
        return super.get(key);
    }

    set(key) {
        if (this.has(key)) { throw new Error(...); }
        return super.set(key);
    }
}

Not sure if I just got the syntax wrong or I'm missing an implementation of some sort. But I get the following error:

Method Map.prototype.forEach called on incompatible reciever