英文:
Why Math is an object in JavaScript?
问题
我最近开始学习JavaScript,所以如果我的疑问对你来说似乎不太合理,我很抱歉。
我看到了一个Math对象的定义,以下是它的代码:
interface Math {
/** 数学常数 e。这是欧拉数,自然对数的底。 */
readonly E: number;
/** 10 的自然对数。 */
readonly LN10: number;
/** 2 的自然对数。 */
readonly LN2: number;
/** e 的以 2 为底的对数。 */
readonly LOG2E: number;
/** e 的以 10 为底的对数。 */
readonly LOG10E: number;
/** 圆周率 π。这是圆的周长与直径的比值。 */
readonly PI: number;
/** 0.5 的平方根,或者等价地,2 的平方根的倒数。 */
readonly SQRT1_2: number;
/** 2 的平方根。 */
readonly SQRT2: number;
/**
* 返回一个数字的绝对值(即不考虑它是正数还是负数的值)。
* 例如,-5 的绝对值与 5 的绝对值相同。
* @param x 需要取绝对值的数字表达式。
*/
abs(x: number): number;
// ... 其他方法 ...
}
当我在Google上搜索它时,我发现它是用TypeScript编写的,这里使用接口来定义了一个名为Math的对象的结构。但对我来说,它似乎与类声明相同,所以为什么我们称Math为一个对象呢?
英文:
I have started learning Javascript recently ,So sorry if my doubts seems not logical to you
I have seen the definition for an Math object , following is the code for it
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;
}
When I searched about it on google , I found it is written in TypeScript here the interface is used to define a structure of an object which is Math , but it seems to me same like a class declaration, So why do we call Math as an object ?
答案1
得分: 1
在JavaScript中,Math
是一个命名空间对象。当你调用Math
上的方法,比如Math.abs(x)
,你实际上在调用被称为静态方法的内容。静态方法是一种常见的面向对象模式,用于实现不应该被实例化的类,最常见的情况是实用工具类。
英文:
In Javascript Math
is a namespace object. When you call methods on Math
, like Math.abs(x)
, you are calling what's known as a static method. Static methods are a common OO pattern for implementing classes that shouldn't be instantiated - most commonly, utility classes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论