Math.round is not a constructor
javascript
Solution
It's nothing special about `Math.round` You can replicate this behaviour on your own functions:
MyClass = function(){};
MyClass.round = function(x){
if(this instanceof MyClass.round)
throw 'TypeError: MyClass.round is not a constructor';
return Math.round(x);
}
console.log(MyClass.round(0.5)); // 1
new MyClass.round(); // 'TypeError: MyClass.round is not a constructor'
In fact you can use a similar pattern to make the `new` keyword optional on your Class:
function MyClass(){
if(!(this instanceof MyClass)) // If not using new
return new MyClass(); // Create a new instance and return it
// Do normal constructor stuff
this.x = 5;
}
(new MyClass()).x === MyClass().x;
As to why `new` doesn't work with built-in functions and methods, this is by design, and documented:
None of the built-in functions described in this clause that are not constructors shall implement the [[Construct]] internal method unless otherwise specified in the description of a particular function. -- http://es5.github.com/#x15
Problem
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?