Alex Rivera | Logout

Why is Math.prototype undefined?

Asked 2012-05-30T16:40:58.383
9

I must be missing something here, because Math.prototype is undefined for me. Why is this? I tried to do something like this:

Math.prototype.randomRange = function(from, to){
    return Math.floor(Math.random() * (to - from + 1) + from);
}

But instead had to do something like this:

Math.randomRange = function(from, to){
    return Math.floor(Math.random() * (to - from + 1) + from);
}

That doesn't feel right, though. Is it just me or should I be doing this another way? I apologize if this is a silly or duplicate question, but I couldn't find anything by searching (exactly two questions turn up when I search SO for "Math.prototype", which is kind of weird).

Edit
Report

1 Answer

2

Math in JavaScript is the equivalent of a static class in other languages. It has no prototype or constructor (the equivalent of, say, writing a class in Java with all static methods and only a private constructor).

For further information, see this StackOverflow question: Is there any practical use of redefining Math.constructor in JavaScript/ActionScript?

Specifically, the accepted answer: https://stackoverflow.com/a/10431309/1403635

answered 2012-05-30T16:44:36.540

Your Answer