为什么Math.prototype未定义? [英] Why is Math.prototype undefined?
问题描述
我必须在这里遗漏一些东西,因为 Math.prototype
是我未定义一>。为什么是这样?我尝试过这样的事情:
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);
}
但不得不这样做:
Math.randomRange = function(from, to){
return Math.floor(Math.random() * (to - from + 1) + from);
}
但这感觉不对。是我还是应该以另一种方式做这件事?如果这是一个愚蠢或重复的问题,我道歉,但我找不到任何搜索结果(当我搜索Math.prototype时,正好有两个问题出现,这有点奇怪)。
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).
推荐答案
Math
函数与其他OO语言中的类级函数类似。构造函数上没有原型对象;没人(我知道)无论如何都使用了构造函数。 (你会用 Math
实例做什么?)
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?)
编辑—它已在评论中指出(我从未想过要检查我猜)数学
无论如何都不是一个函数。在浏览器控制台中键入 Math()
,您将收到错误。
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.
这篇关于为什么Math.prototype未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!