ๆธๅญ(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
Date
objects. - "get" methods, for getting date and time values from
Date
objects. - "to" methods, for returning string values from
Date
objects. - parse and UTC methods, for parsing
Date
strings.
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."ใ