Consider the following:
x = function () {}
p = new x()
console.log(p) // ok
Math.z = function () {}
p = new Math.z()
console.log(p) // ok
p = new Math.round()
console.log(p) // TypeError: function round() { [native code] } is not a constructor
So I can use new with my own functions, but not with Math.round. What makes it so special? Is this documented somewhere?
It’s nothing special about
Math.roundYou can replicate this behaviour on your own functions:In fact you can use a similar pattern to make the
newkeyword optional on your Class:As to why
newdoesn’t work with built-in functions and methods, this is by design, and documented: