georg
georg

Reputation: 215029

Math.round is not a constructor

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?

Upvotes: 3

Views: 866

Answers (3)

Paul
Paul

Reputation: 141887

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

Upvotes: 8

Derek 朕會功夫
Derek 朕會功夫

Reputation: 94359

It is a method, that's why you can not put new before it.

ps:

new alert()        //TypeError: Illegal invocation
new console.log()  //TypeError: Illegal invocation
new function(){}   //OK

Upvotes: 2

Gwyn Howell
Gwyn Howell

Reputation: 5424

Math is a class, so you can create an object from it. Math.round is a method of Math. You cannot create an object from a method.

Upvotes: 1

Related Questions