æ¸å(Numbers)
å¨ JavaScript ä¸, Number æä½¿ç¨çæ¨æºä¾ç § double-precision 64-bit binary format IEEE 754 (i.e. number çå鿝 -(2^53 -1) å° 2^53 -1)ãæ´æ¸æ¯æ²æç¹å®çé¡åã
æ¤å¤éå¯ä»¥é¡¯ç¤ºæµ®é»æ¸ï¼ä¸ç¨®ç¬¦èæ¸å¼: +Infinityï¼ -Infinityï¼ and NaN (not-a-number)ã
BigInt æ¯ Javascript ææ°çåè½ï¼å®å¯ä»¥è¡¨ç¤ºä¸åå¾å¤§çæ´æ¸ãä½¿ç¨ BigIntéè¦æ³¨æä¸é»ï¼BigInt åNumberä¸è½å¨åä¸å operation æ··ç¨éæç¶ç¨ Math ç©ä»¶æä¸è½ä½¿ç¨BigIntã
è«åç § JavaScript data types and structures ä¾å徿´å¤è©³ç´°è³æã
ä½ å¯ä»¥ç¨å種é²å¶è¡¨ç¤ºæ¸åï¼åé²å¶ (decimal)ï¼äºé²å¶ (binary)ï¼å «é²å¶ (octal) 以ååå é²å¶ (hexadecimal)ã
åé²å¶æ¸å¼
1234567890;
42;
// 以é¶çºéé æè¦å°å¿ï¼
0888; // 888 è§£æçº åé²å¶æ¸å¼
0777; // å¨ non-strict 模å¼ä¸å°è§£ææå
«é²å¶ (çåæ¼åé²å¶ç 511)
è«æ³¨æï¼åé²ä½æ¸åå
許第ä¸åæ¸åè¨çºé¶(0)ç話ï¼åææ¯å¾é¢æ¥çæ¸åå¿
é è¦æä¸åæ¸åå¤§æ¼ 8(ä¾å¦è¼¸å
¥ 0888 çµæææ¯ 888ï¼è¼¸å
¥ 068 çµæææ¯ 68)ï¼ä¸ç¶åæè¢«è½æï¼é²ä½(ä¾å¦ 0777 çµæææ¯ 511ï¼è¼¸å
¥ 063 çµæææ¯ 51)ã
äºé²å¶æ¸å¼
äºé²å¶æ¸å¼ä»¥ 0 çºéé 並è·èä¸å大寫æå°å¯«çè±æåæ¯ ãBã (0b æ 0B)ã妿 0b å¾é¢æ¥èçæ¸å䏿¯ 0 æ 1ï¼é£æä¸åº SyntaxError(èªæ³é¯èª¤): "Missing binary digits after 0b"ã
var FLT_SIGNBIT = 0b10000000000000000000000000000000; // 2147483648
var FLT_EXPONENT = 0b01111111100000000000000000000000; // 2139095040
var FLT_MANTISSA = 0b00000000011111111111111111111111; // 8388607
å «é²å¶æ¸å¼
å
«é²å¶æ¸å¼ä»¥ 0 çºéé ã妿 0 å¾é¢çæ¸åè¶
åº 0 å° 7 éåç¯åï¼å°æè¢«è§£ææåé²å¶æ¸å¼ã
var n = 0755; // 493
var m = 0644; // 420
Strict mode in ECMAScript 5 forbids octal syntax. Octal syntax isn't part of ECMAScript 5, but it's supported in all browsers by prefixing the octal number with a zero: 0644 === 420 and"\045" === "%". In ECMAScript 2015, octal numbers are supported if they are prefixed with 0o, e.g.:
var a = 0o10; // ES2015: 8
åå é²å¶æ¸å¼
åå
é²å¶æ¸å¼ä»¥ 0 çºéé 並è·èä¸å大寫æå°å¯«çè±æåæ¯ ãXã(0x æ 0X)ã妿 0b å¾é¢æ¥èçå¼è¶
åºç¯å (0123456789ABCDEF)ï¼é£æä¸åº SyntaxError(èªæ³é¯èª¤):"Identifier starts immediately after numeric literal"ã
0xfffffffffffffffff; // 295147905179352830000
0x123456789abcdef; // 81985529216486900
0xa; // 10
ææ¸éç®
1e3; // 1000
2e6; // 2000000
0.1e2; // 10
Number ç©ä»¶
The built-in Number object has properties for numerical constants, such as maximum value, not-a-number, and infinity. You cannot change the values of these properties and you use them as follows:
var biggestNum = Number.MAX_VALUE;
var smallestNum = Number.MIN_VALUE;
var infiniteNum = Number.POSITIVE_INFINITY;
var negInfiniteNum = Number.NEGATIVE_INFINITY;
var notANum = Number.NaN;
You always refer to a property of the predefined Number object as shown above, and not as a property of a Number object you create yourself.
ä¸é¢éå¼µè¡¨æ ¼æ´çäº Number ç©ä»¶ç屬æ§
Number ç屬æ§
| å±¬æ§ | æè¿° |
|---|---|
Number.MAX_VALUE |
å¯è¡¨ç¤ºçæå¤§æ¸å¼ |
Number.MIN_VALUE |
å¯è¡¨ç¤ºçæå°æ¸å¼ |
Number.NaN |
表示ã鿏å¼ãï¼Not-A-Numberï¼çæ¸å¼ |
Number.NEGATIVE_INFINITY |
Special negative infinite value; returned on overflow |
Number.POSITIVE_INFINITY |
Special positive infinite value; returned on overflow |
Number.EPSILON |
Difference between one and the smallest value greater than one that can be represented as a Number. |
Number.MIN_SAFE_INTEGER |
å¯ä»¥å¨ JavaScript ä¸å®å ¨è¡¨ç¤ºçæå°æ¸å¼ã |
Number.MAX_SAFE_INTEGER |
å¯ä»¥å¨ JavaScript ä¸å®å ¨è¡¨ç¤ºçæå¤§æ¸å¼ã |
| æ¹æ³ | æè¿° |
|---|---|
Number.parseFloat() |
åä¸²è½æææµ®é»æ¸ã çåæ¼å
¨åå½å¼ parseFloat() ã |
Number.parseInt() |
以æå®çåºæ¸å°åä¸²è½æææ´æ¸ã çåæ¼å
¨åå½å¼ parseInt() ã |
Number.isFinite() |
å¤å®çµ¦å®ç弿¯ä¸æ¯ä¸åæéæ¸ã |
Number.isInteger() |
å¤å®çµ¦å®ç弿¯ä¸æ¯ä¸åæ´æ¸ |
Number.isNaN() |
Determines whether the passed value is NaN. More robust version of the original global isNaN(). |
Number.isSafeInteger() |
Determines whether the provided value is a number that is a safe integer. |
The Number prototype provides methods for retrieving information from Number objects in various formats. The following table summarizes the methods of Number.prototype.
| æ¹æ³ | æè¿° |
|---|---|
toExponential() |
Returns a string representing the number in exponential notation. |
toFixed() |
Returns a string representing the number in fixed-point notation. |
toPrecision() |
Returns a string representing the number to a specified precision in fixed-point notation. |
Math ç©ä»¶
The built-in Math object has properties and methods for mathematical constants and functions. For example, the Math object's PI property has the value of pi (3.141...), which you would use in an application as
Math.PI;
Similarly, standard mathematical functions are methods of Math. These include trigonometric, logarithmic, exponential, and other functions. For example, if you want to use the trigonometric function sine, you would write
Math.sin(1.56);
Note that all trigonometric methods of Math take arguments in radians.
The following table summarizes the Math object's methods.
| æ¹æ³ | æè¿° |
|---|---|
abs() |
çµå°å¼ |
sin(), cos(), tan() |
ä¸è§å½æ¸; 弿¸ä»¥å¼³åº¦è¡¨ç¤º |
asin(), acos(), atan(), atan2() |
åä¸è§å½æ¸; åå³å¼ä»¥å¼³åº¦è¡¨ç¤º |
sinh(), cosh(), tanh() |
鿲彿¸; 弿¸ä»¥ hyperbolic angle 表示 |
asinh(), acosh(), atanh() |
å鿲彿¸; åå³å¼ä»¥ hyperbolic angle 表示 |
pow(), exp(), expm1(), log10(), log1p(), log2() |
ææ¸åå°æ¸å½å¼ |
floor(), ceil() |
åå³å°æ¼çæ¼/大æ¼çæ¼æå®æ¸åçæå¤§/æå°æ´æ¸ |
min(), max() |
Returns lesser or greater (respectively) of comma separated list of numbers arguments |
random() |
åå³ä¸å仿¼ 0 å° 1 ä¹éçæ¸å¼ |
round(), fround(), trunc(), |
Rounding and truncation functions. |
sqrt(), cbrt(), hypot() |
Square root, cube root, Square root of the sum of square arguments. |
sign() |
The sign of a number, indicating whether the number is positive, negative or zero. |
clz32(), imul() |
Number of leading zero bits in the 32-bit binary representation. The result of the C-like 32-bit multiplication of the two arguments. |
Unlike many other objects, you never create a Math object of your own. You always use the built-in Math object.
Date ç©ä»¶
JavaScript æ²ææè¬æ¥æ(date)çæ¸æåæ
(data type)ãä½ å¯ä»¥ä½¿ç¨ Date ç©ä»¶åå
¶æ¹æ³å»è¨å®æ¥æè·æé便»¿è¶³ä½ çéæ± ãDate ç©ä»¶æå¤§éçè¨å®å徿使¥æçæ¹æ³(method)ï¼ä½å®æ²æå±¬æ§ã
JavaScript èçæ¥æçæ¹å¼è· Java é¡ä¼¼ãéå ©åèªè¨æè¨±å¤ä¸æ¨£ç date æ¹æ³ï¼ä¸é½å°æ¥æå²åçºå¾ 1970 å¹´ 1 æ 1 è 0 æ 0 å 0 ç§ä»¥ä¾çæ¯«ç§æ¸(millisecond)ã
Date ç©ä»¶ç¯åæ¯ -100,000,000 days to 100,000,000 days 以 1970 å¹´ 1 æ 1 è 0 æ 0 å 0 ç§ UTC çºåºæºã
åµå»ºä¸åDateç©ä»¶:
var dateObjectName = new Date([parameters]);
å¨é裡åµå»ºä¸ååçºdateObjectName ç Date ç©ä»¶ï¼å®å¯ä»¥æ¯ä¸åæ°çç©ä»¶ææ¯æå以åå¨çç©ä»¶ç¶ä¸ç屬æ§ã
Calling Date without the new keyword returns a string representing the current date and time.
The parameters in the preceding syntax can be any of the following:
- Nothing: creates today's date and time. For example,
today = new Date();. - A string representing a date in the following form: "Month day, year hours:minutes:seconds." For example,
var Xmas95 = new Date("December 25, 1995 13:30:00"). If you omit hours, minutes, or seconds, the value will be set to zero. - A set of integer values for year, month, and day. For example,
var Xmas95 = new Date(1995, 11, 25). - A set of integer values for year, month, day, hour, minute, and seconds. For example,
var Xmas95 = new Date(1995, 11, 25, 9, 30, 0);.
Date çæ¹æ³
The Date object methods for handling dates and times fall into these broad categories:
- "set" methods, for setting date and time values in
Dateobjects. - "get" methods, for getting date and time values from
Dateobjects. - "to" methods, for returning string values from
Dateobjects. - parse and UTC methods, for parsing
Datestrings.
With the "get" and "set" methods you can get and set seconds, minutes, hours, day of the month, day of the week, months, and years separately. There is a getDay method that returns the day of the week, but no corresponding setDay method, because the day of the week is set automatically. These methods use integers to represent these values as follows:
- Seconds and minutes: 0 å° 59
- Hours: 0 å° 23
- Day: 0 (æææ¥) å° 6 (ææå )
- Date: 1 å° 31 (éåæç第幾天)
- Months: 0 (䏿) å° 11 (åäºæ)
- Year: years since 1900
èä¾ï¼åè¨ä½ å®ç¾©äºä¸åæ¥æå¦ä¸ï¼
var Xmas95 = new Date("December 25, 1995");
é£ Xmas95.getMonth() å°æåå³ 11ï¼ Xmas95.getFullYear() æåå³ 1995ã
getTime å setTime éå
©åæ¹æ³å°æ¼æ¯è¼æ¥ææå¹«å©ã The getTime method returns the number of milliseconds since January 1, 1970, 00:00:00 for a Date object.
For example, the following code displays the number of days left in the current year:
var today = new Date();
var endYear = new Date(1995, 11, 31, 23, 59, 59, 999); // Set day and month
endYear.setFullYear(today.getFullYear()); // Set year to this year
var msPerDay = 24 * 60 * 60 * 1000; // Number of milliseconds per day
var daysLeft = (endYear.getTime() - today.getTime()) / msPerDay;
var daysLeft = Math.round(daysLeft); //returns days left in the year
This example creates a Date object named today that contains today's date. It then creates a Date object named endYear and sets the year to the current year. Then, using the number of milliseconds per day, it computes the number of days between today and endYear, using getTime and rounding to a whole number of days.
The parse method is useful for assigning values from date strings to existing Date objects. For example, the following code uses parse and setTime to assign a date value to the IPOdate object:
var IPOdate = new Date();
IPOdate.setTime(Date.parse("Aug 9, 1995"));
ç¯ä¾
ä¸é¢éåç¯ä¾ï¼JSClock() éåå½å¼å°æä»¥æ¸ä½æéçæ ¼å¼å峿éã
function JSClock() {
var time = new Date();
var hour = time.getHours();
var minute = time.getMinutes();
var second = time.getSeconds();
var temp = "" + (hour > 12 ? hour - 12 : hour);
if (hour == 0) temp = "12";
temp += (minute < 10 ? ":0" : ":") + minute;
temp += (second < 10 ? ":0" : ":") + second;
temp += hour >= 12 ? " P.M." : " A.M.";
return temp;
}
JSClock éåå½å¼æå
建ç«ä¸ååçº time ç Date ç©ä»¶; å çºæ²ææä¾ä»»ä½å¼æ¸ï¼æä»¥ææ¾å
¥ç®åçæ¥æåæéãæ¥èå¼å« getHours ã getMinutes 以å getSeconds éä¸åæ¹æ³å°ç¾å¨çæãå以åç§å奿å®çµ¦ hour ã minute 以å second éä¸åè®æ¸ã
æ¥èçåè¡æä»¤å°æå»ºç«ä¸åæéçå串ã第ä¸è¡çæä»¤å»ºç«äºä¸åè®æ¸ tempï¼ä»¥æ¢ä»¶éç®å¼æå®å¼; 妿 hour å¤§æ¼ 12ï¼é£å°±æå® (hour - 12)ï¼ä¸ç¶æç´æ¥æå® hourï¼ ä½å¦æ hour çæ¼ 0 ï¼ åæ¹çº 12ã
æ¥èä¸ä¸è¡å° minute å å° temp ä¸ã妿 minute å°æ¼ 10, åæå¨éå æè£ä¸ä¸åé¶; ä¸ç¶ç話æç´æ¥å ä¸åèåå鿏ãç§æ¸ä¹æ¯ä»¥å樣ç使³éå å° temp ä¸ã
æå¾ï¼å¤æ· hour æ¯ä¸æ¯å¤§æ¼çæ¼ 12 ï¼å¦ææ¯å°±å¨ temp å ä¸ "P.M." ï¼ä¸ç¶å°±å ä¸ "A.M."ã