Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is Math.prototype undefined?

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

like image 928
Elliot Bonneville Avatar asked May 30 '12 16:05

Elliot Bonneville


1 Answers

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.

like image 83
Pointy Avatar answered Oct 20 '22 00:10

Pointy