我最近开始学习Javascript,如果我的怀疑对你来说不合逻辑,很抱歉
我看过Math对象的定义,下面是它的代码
interface Math {
/** The mathematical constant e. This is Euler's number, the base of natural logarithms. */
readonly E: number;
/** The natural logarithm of 10. */
readonly LN10: number;
/** The natural logarithm of 2. */
readonly LN2: number;
/** The base-2 logarithm of e. */
readonly LOG2E: number;
/** The base-10 logarithm of e. */
readonly LOG10E: number;
/** Pi. This is the ratio of the circumference of a circle to its diameter. */
readonly PI: number;
/** The square root of 0.5, or, equivalently, one divided by the square root of 2. */
readonly SQRT1_2: number;
/** The square root of 2. */
readonly SQRT2: number;
/**
* Returns the absolute value of a number (the value without regard to whether it is positive or negative).
* For example, the absolute value of -5 is the same as the absolute value of 5.
* @param x A numeric expression for which the absolute value is needed.
*/
abs(x: number): number;
/**
* Returns the arc cosine (or inverse cosine) of a number.
* @param x A numeric expression.
*/
acos(x: number): number;
/**
* Returns the arcsine of a number.
* @param x A numeric expression.
*/
asin(x: number): number;
/**
* Returns the arctangent of a number.
* @param x A numeric expression for which the arctangent is needed.
*/
atan(x: number): number;
/**
* Returns the angle (in radians) from the X axis to a point.
* @param y A numeric expression representing the cartesian y-coordinate.
* @param x A numeric expression representing the cartesian x-coordinate.
*/
atan2(y: number, x: number): number;
/**
* Returns the smallest integer greater than or equal to its numeric argument.
* @param x A numeric expression.
*/
ceil(x: number): number;
/**
* Returns the cosine of a number.
* @param x A numeric expression that contains an angle measured in radians.
*/
cos(x: number): number;
/**
* Returns e (the base of natural logarithms) raised to a power.
* @param x A numeric expression representing the power of e.
*/
exp(x: number): number;
/**
* Returns the greatest integer less than or equal to its numeric argument.
* @param x A numeric expression.
*/
floor(x: number): number;
/**
* Returns the natural logarithm (base e) of a number.
* @param x A numeric expression.
*/
log(x: number): number;
/**
* Returns the larger of a set of supplied numeric expressions.
* @param values Numeric expressions to be evaluated.
*/
max(...values: number[]): number;
/**
* Returns the smaller of a set of supplied numeric expressions.
* @param values Numeric expressions to be evaluated.
*/
min(...values: number[]): number;
/**
* Returns the value of a base expression taken to a specified power.
* @param x The base value of the expression.
* @param y The exponent value of the expression.
*/
pow(x: number, y: number): number;
/** Returns a pseudorandom number between 0 and 1. */
random(): number;
/**
* Returns a supplied numeric expression rounded to the nearest integer.
* @param x The value to be rounded to the nearest integer.
*/
round(x: number): number;
/**
* Returns the sine of a number.
* @param x A numeric expression that contains an angle measured in radians.
*/
sin(x: number): number;
/**
* Returns the square root of a number.
* @param x A numeric expression.
*/
sqrt(x: number): number;
/**
* Returns the tangent of a number.
* @param x A numeric expression that contains an angle measured in radians.
*/
tan(x: number): number;
}
当我在谷歌上搜索它的时候,我发现它是用TypeScript写的,这里的接口是用来定义一个对象的结构的,这个对象是Math,但在我看来,它就像一个类声明,那么为什么我们要把Math作为一个对象来调用呢?
1条答案
按热度按时间whitzsjs1#
在Javascript中,
Math
是namespace object。当你调用Math
上的方法时,比如Math.abs(x)
,你调用的是static method。静态方法是一种常见的OO模式,用于实现不应该被示例化的类--最常见的是实用程序类。