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
There is no drawback in extending Number.prototype
other than confusing other people. What's the point? What is better in using value.round()
instead of Math.round(value)
?
There are several good reasons for the Math
object:
Math.round("5")
works whereas value.round()
won't work when value is a string (for example, the value of a textbox)Math.min()
or Math.max()
. Or do you want to use it like a.max(b)
?Math.PI
or the function Math.random()
.