Học JavaScript qua ví dụ part 28

Các đối tượng toán học Các đối tượng toán học cho phép bạn làm việc với các phép tính số học tiên tiến hơn, chẳng hạn như căn bậc hai, hàm lượng giác, logarit, và số ngẫu nhiên, hơn là pro-vided bởi các nhà khai thác số cơ bản. Nếu bạn đang làm phép tính đơn giản, bạn sẽ không cần | The Math Object 241 EXPLANATION 1 The function called weekDay is defined. 2 The variable now is assigned a number representing the day of the week where 0 is Sunday. 3 A new Array object called names is created. It will contain seven elements. Each element will be assigned the name of the weekday . Sunday . 4 The value in now a number between 0 and 6 will be used as an index in the names array. If now is 6 then the value of names 6 Saturday will be returned. 5 A prototype method called DayOfWeek is assigned the name of the function weekDay. Now the Date object has a new method that will be inherited by all objects created from the Date constructor. The capabilities of the Date object have been extended to provide a method that will return the name of the weekday. See Chapter 8 for more on prototypes. 6 A new Date object is created with the Date constructor function. 7 The new prototype method is called and returns the string value of today s date Saturday see Figure . Today is Saturday. Figure The day is converted to a string using a prototype. The Math Object The Math object allows you to work with more advanced arithmetic calculations such as square root trigonometric functions logarithms and random numbers than are provided by the basic numeric operators. If you are doing simple calculations you really won t need it. Unlike other objects you don t have to create an instance of the Math object with the new keyword. It is a built-in object and has a number of properties see Table and methods see Table . The Math object always starts with an uppercase M. Table Math Object Properties Property Value Description Euler s constant the base of natural logarithms Natural log of 2 Natural log of 10 Continues From the Library of 242 Chapter 9 JavaScript Core Objects Table Math Object Properties continued Property Value Description .

Không thể tạo bản xem trước, hãy bấm tải xuống
TỪ KHÓA LIÊN QUAN
TÀI LIỆU MỚI ĐĂNG
11    81    2    17-06-2024
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.