內置物件
- JavaScript中的物件有三種:自定義物件、 內置物件、瀏覽器物件
- ECMAScript中的物件:自定義物件、內置物件
- 內置物件:Math、Array、Date、... ...
學習方法:
- 最常用的屬性和方法
- 查檔案:MDN
Math物件
Math不是一個建構式,里面提供的是靜態成員
屬性
Math.PI
Math.PI表示一個圓的周長與直徑的比例,約為 3.14159
console.log(Math.PI); // 3.141592653589793
// 計算圓的面積
function getCircleArea(radius) {
return 2 * Math.PI * radius;
}
var radius = 2;
console.log(getCircleArea(radius)); // 12.566370614359172
方法
Math.ceil()
Math.ceil()函式回傳大于或等于一個給定數字的最小整數,
console.log(Math.ceil(0.95)); // 1
console.log(Math.ceil(4)); // 4
console.log(Math.ceil(7.005)); // 8
console.log(Math.ceil(-7.005)) // -7
Math.floor()
Math.floor()回傳小于或等于一個給定數字的最大整數
console.log(Math.floor(45.95)); // 45
console.log(Math.floor(45.05)); // 45
console.log(Math.floor(4)); // 4
console.log(Math.floor(-45.05)); // -46
console.log(Math.floor(-45.95)); // -46
Math.max()
Math.max()函式回傳一組數中最大值
console.log(Math.max(1, 5, 8, 7, 6, 8, 5)); // 8
Math.min()
Math.max()函式回傳一組數中最小值
console.log(Math.max(1, 5, 8, 7, 6, 8, 5)); // 1
Math.pow()
Math.pow()函式回傳基數(base)的指數(exponent)次冪,
// 語法
Math.pow(base, exponent) // base基數 exponent指數
// 案例
console.log(Math.pow(2, 2)) // 4
Math.random()
Math.random()函式回傳一個從0到1的隨機浮點數,包括0,不包括1
function getRandom() {
return Math.random();
}
var num1 = getRandom(); // 隨機浮點數
Math.round()
Math.round()函式回傳一個數字四舍五入后最接近的整數,
console.log(Math.round(20.49)); // 20
console.log(Math.round(20.5)); // 21
console.log(Math.round(-20.49)); // -20
console.log(Math.round(-20.5)); // -20
console.log(Math.round(-20.51)); // -21
注意:
與很多其他語言中的round()函式不同,Math.round()并不總是舍入到遠離0的方向(尤其是在負數的小數部分恰好等于0.5的情況下),
案例
- 求10-20之間的隨機整數. [10, 20]
function getRandom(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1) + min);
}
console.log(getRandom(10, 20));
- 隨機生成RGB顏色
function getRGBValue() {
var RValue = https://www.cnblogs.com/chezeze/p/Math.floor(Math.random() * (255 + 1));
var GValue = Math.floor(Math.random() * (255 + 1));
var BValue = Math.floor(Math.random() * (255 + 1));
var RGBValue ="RGB" + "(" + RValue + "," + GValue + "," + BValue + ")";
return RGBValue;
}
console.log(getRGBValue());
- 模擬實作
Math.max()/Math.min()
var MyMath = {
max: function () {
var max = arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (max < arguments[i]) {
max = arguments[i];
}
}
return max;
},
min: function () {
var min= arguments[0];
for (var i = 1; i < arguments.length; i++) {
if (min > arguments[i]) {
min = arguments[i];
}
}
return min;
}
}
console.log(MyMath.max(1,5,2,5,45,24,8,4)); // 45
console.log(MyMath.min(1,5,2,5,45,24,8,4)); // 1
Date物件
MDN檔案鏈接:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Date
Date是一個建構式,首先要通過 new Date() 來創建日期示例(物件),實體成員
var d = new Date()
// 直接列印 d 是一個 GMT 格林威治時間 世界標準時間 GMT+0800(中國標準時間)
console.log(d); // Wed Jul 07 2021 11:25:01 GMT+0800 (中國標準時間)
// 列印 d 的 valueOf() 是距離1970-1-1相差的毫秒數,如果在1970年以前,列印的是負數值
console.log(d.valueOf()); // 1625628301304
- 格林威治時間一般指世界時間
- 北京時差:現在格林威治時間比北京時間晚8小時
Date建構式的四種用法
// 空建構式,獲取的是當前時間物件
new Date();
// 建構式中傳入毫秒值
new Date(value);
// 建構式中傳入日期形式的字串(不建議使用)
new Date(dateString);
// 建構式中傳入日期與時間的每一個成員(至少提供年和月兩個成員) , 月份是從0開始
new Date(year, monthIndex [, day [, hours [, minutes [, seconds [, milliseconds]]]]]);
獲取日期物件的毫秒值
從1970年1月1日0時0分0秒(UTC,即協調世界時)到該日期物件所代表時間的毫秒數,
// 1. valueOf()方法,該方法通常在 JavaScript 內部被呼叫,一般不用在代碼中顯式呼叫
var d = new Date();
console.log(d.valueOf());
// 2. getTime()方法,這個方法的功能和 valueOf() 方法一樣
var d = new Date();
console.log(d.getTime());
// 3. Date.now(),Date的靜態成員
var d = Date.now();
console.log(d);
// 4. 使用運算子“+”取正的方法獲取物件毫秒值
var d = + new Date();
console.log(d);
Date物件的常用方法
隱式呼叫方法
var d = new Date();
// 1. toString(),轉換成字串
congsole.log(d.toString())
// 2. valueOf(),獲取毫秒值
congsole.log(d.valueOf())
日期格式化方法
下面格式化日期的方法,在不同瀏覽器可能表現不一致,一般不用
var d = new Date();
// 1. toDateString(),轉換成美式英語日期部分字串
congsole.log(d.toDateString())
// 2. toLocaleDateString(),轉換成本地日期部分字串
congsole.log(d.toLocaleDateString())
// 3. toLocaleTimeString(), 轉換成本地時間部分字串
congsole.log(d.toLocaleTimeString())
獲取日期指定部分
var d = new Date();
// 1. getTime(),回傳毫秒數,和valueOf()結果一樣
congsole.log(d.getTime())
// 2. getMilliseconds(),回傳毫秒數
congsole.log(d.getMilliseconds())
// 3. getseconds(), 回傳秒數,0-59
congsole.log(d.getseconds())
// 4. getMinutes(), 回傳分鐘數,0-59
congsole.log(d.getMinutes())
// 5. getHours(), 回傳小時數,0-23
congsole.log(d.getHours())
// 6. getDay(),回傳星期,0代表周日,6代表周六
// 7. getDate(), 回傳天數,1-31
congsole.log(d.getDate())
// 8. getMonth(), 回傳月數,0-11,0代表1月份,11代表12月份
congsole.log(d.getMonth())
// 9. getFullYear(), 回傳4位數年份
congsole.log(d.getFullYear())
格式化日期案例
function formatDate(date) {
if (!(date instanceof Date)) {
console.error("date不是日期物件")
return;
}
var year = date.getFullYear();
var month = date.getMonth() + 1;
var day = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
month = month > 10 ? month : "0" + month;
day = day > 10 ? day : "0" + day;
hours = hours > 10 ? hours : "0" + hours;
minutes = minutes > 10 ? minutes : "0" + minutes;
seconds = seconds > 10 ? seconds : "0" + seconds;
return year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
}
var d = new Date();
var currentTime = formatDate(d);
console.log(currentTime);
計算日期差
案例:計算時間差,回傳相差的天/時/分/秒
function fn(start, end) {
var interval = d2 - d1;
interval /= 1000;
var day = Math.round(interval / 60 / 60 / 24);
var hours = Math.round(interval / 60 / 60 % 24);
var minutes = Math.round(interval / 60 % 60);
var seconds = Math.round(interval % 60);
return {
day:day,
hours:hours,
minutes:minutes,
seconds:seconds,
}
}
var d1 = new Date();
var d2 = new Date(2021, 6, 8);
var intervalTime = fn(d1, d2);
console.log(intervalTime);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/289010.html
標籤:JavaScript
