Extending Number.prototype in javascript and the Math object?

后端 未结 8 1767
孤城傲影
孤城傲影 2021-02-05 03:11

I\'ve always wondered why Javascript has the global Math object instead of giving numbers their own methods. Is there a good reason for it?

Also are there any drawbacks

8条回答
  •  滥情空心
    2021-02-05 03:22

    Try doing 123.round();

    Your javascript console will throw a few hundred of errors to your eyes :P, nah ...

    You can do:

    Number.prototype.x then: (123).x(); but never 123.x();

提交回复
热议问题