0

How come when I create a Map it requires new and it is not required on an Object?

var o = new Object();
var m = new Map();

var constructorObject = o.constructor;
var constructorMap = m.constructor;

var newObject = constructorObject();
var newMap = constructorMap();  //splat

That last line produces,

TypeError: Constructor Map requires 'new'
    at Map (native)
user157251
  • 64,489
  • 38
  • 208
  • 350

1 Answers1

0

This simply depends on the implementation of the Map feature. You get this error in Chrome, while in Nightly calling Map(); returns you a beautiful brand new Map.

Keep in mind that the fact that you can create objects without using the new operator is just more or less useless (one could argue that it serves a purpose for the Object constructor as it doesn't simply return a new object, but an object which type matches the type of argument you passed). You could do the same for any constructor:

function Test() {
  if(!(this instanceof Test)) return new Test;
}
Test(); // equivalent to *new Test;*
Loamhoof
  • 8,163
  • 24
  • 29