Math 是一个内置对象, 它具有数学常数和函数的属性和方法。不是一个函数对象。
与其它全局对象不同的是, Math 不是一个构造器. Math 的所有属性和方法都是静态的. 你用到的常数pi可以用 Math.PI 表示,用 x 作参数 Math.sin(x)调用sin函数. JavaScript中的常数, 是以全精度的实数定义的.
Math.EMath.LN2Math.LN10Math.LOG2EMath.LOG10EMath.PIMath.SQRT1_2Math.SQRT2Object:需要注意的是三角函数(sin(), cos(), tan(),asin(), acos(), atan(), atan2())是以弧度返回值的。可以通过除法(Math.PI / 180)把弧度转换为角度,也可以通过其他方法来转换。
需要注意的是很多数学函数都有一个精度,并且精度在不同环境下也是不相同的。这就意味着不同的浏览器会给出不同的结果,甚至相同的 JS 引擎在不同的OS或者架构下也会给出不同的结果。
Math.abs(x)Math.acos(x)Math.acosh(x) Math.asin(x)Math.asinh(x) Math.atan(x)Math.atanh(x) Math.atan2(y, x)Math.cbrt(x) Math.ceil(x)Math.clz32(x) Math.cos(x)Math.cosh(x) Math.exp(x)Math.expm1(x) Math.floor(x)Math.fround(x) Math.hypot([x[,y[,…]]]) Math.imul(x) Math.log(x)Math.log1p(x) Math.log10(x) Math.log2(x) Math.max([x[,y[,…]]])Math.min([x[,y[,…]]])Math.pow(x,y)Math.random()Math.round(x)Math.sign(x) Math.sin(x)Math.sinh(x) Math.sqrt(x)Math.tan(x)Math.tanh(x) Math.toSource() "Math".Math.trunc(x) Object:| 规范 | 状态 | 说明 |
|---|---|---|
| ECMAScript 1st Edition. | Standard | 初始定义。在 JavaScript 1.1 中实现。 |
| ECMAScript 5.1 (ECMA-262) Math |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) Math |
Standard | New methods log10(), log2(), log1p(), expm1(), cosh(), sinh(), tanh(), acosh(), asinh(), atanh(), hypot(), trunc(), sign(), imul(), fround(), cbrt() and clz32() added. |
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| Feature | Android | Chrome for Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
|---|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |