最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Math.round is not a constructor - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked May 7, 2012 at 6:51 georggeorg 215k56 gold badges322 silver badges400 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 8

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./#x15

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

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.

发布评论

评论列表(0)

  1. 暂无评论