The following doesn’t work:
const x = Object.create(Set.prototype)
x.has(1) // crashes with Method Set.prototype.has called on incompatible receiver #<Set>
const y = Object.create(Map.prototype)
y.get(1) // crashes with Method Set.prototype.has called on incompatible receiver #<Map>
I was hoping for a type-independent way of creating empty objects.
Why doesn’t the above work like that?
2
Answers
A better way to do it:
call
new ctor
wherector
is the object's contructor.Object.create
creates a new ordinary object inheriting from the passed argument. It does nothing to initialise that object, like call the constructor.In the case of
Set
andMap
, calling the constructor essential, as that creates the internal slots which the collection needs to function (in particular, to store elements). These internal slots must be constructed together with the object, they cannot be added afterwards. This works only by calling the constructor with thenew
keyword.When you use
Object.create
, the objects inherithas
andget
methods from the respective prototype object just fine, but when calling them their receiver (this
value) is the ordinary object which is missing those internal slots that would make it a trueSet
orMap
, and the methods throw an exception.