Elliot Bonneville - 3 months ago 13

Javascript Question

I must be missing something here, because

`Math.prototype`

`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).

Answer

The `Math`

functions are like class-level functions in other OO languages. There's no prototype object on the constructor; nobody (I know of) uses the constructor anyway. (What would you do with a `Math`

instance?)

*edit* — it's been pointed out in a comment (and it never occurred to me to check I guess) that `Math`

isn't a function anyway. Type `Math()`

in your browser console and you'll get an error.