一,JavaScript內(nèi)置對(duì)象
Array:用于在單獨(dú)的變量名中存儲(chǔ)一系列的值
String:用于支持對(duì)字符串的處理
Math:用于執(zhí)行常用的數(shù)學(xué)任務(wù),包含若干個(gè)數(shù)字常量和函數(shù)
Date:用于操作日期和時(shí)間
二,Array對(duì)象
創(chuàng)建數(shù)組
new Array(); // 創(chuàng)建一個(gè)空數(shù)組
new Array(size); // 創(chuàng)建的時(shí)候給個(gè)大小
new Array(element0, element1, …, elementN); // 創(chuàng)建的時(shí)候指定元素,賦值
為數(shù)組元素賦值
<script>
// 方法一
var shuzu1 = new Array("apple","orange","peach","banana");
// 方法二
var shuzu2 = ["apple","orange","peach","banana"];
// 方法三
var shuzu3 = new Array(4);
shuzu3[0] = "apple";
shuzu3[1] = "orange";
shuzu3[2] = "peach";
shuzu3[3] = "banana";
</script>
訪問數(shù)組
數(shù)組名[下標(biāo)]
console.log(shuzu3[2]);
2.1,常用屬性和方法
類別 | 名稱 | 描述 |
---|---|---|
屬性 | length | 設(shè)置或返回?cái)?shù)組中元素的數(shù)目 |
方法 | join() | 把數(shù)組的所有元素放入一個(gè)字符串,通過逗號(hào)或指定的分隔符進(jìn)行分隔 |
方法 | sort() | 對(duì)數(shù)組排序 |
方法 | push() | 向數(shù)組末尾添加一個(gè)或更多元素,并返回新的長度 |
方法 | forEach() | 遍歷數(shù)組,forEach()方法不會(huì)直接修改原始數(shù)組,但是回調(diào)函數(shù)可能會(huì)修改 |
forEach()方法
array.forEach(callback[, thisArg]);
callback參數(shù):為數(shù)組中的每個(gè)元素執(zhí)行的函數(shù)
callback(currentValue[, index[, array]])
currentValue:數(shù)組中正在處理的當(dāng)前元素
index:可選,數(shù)組中正在處理的當(dāng)前元素的索引
array:可選,forEach()方法正在操作的數(shù)組
thisArg參數(shù):可選,callback函數(shù)中的this可以引用的對(duì)象
示例:
<script>
var arr1 = ["apple","orange","peach"];
console.log(arr1.length);
arr1.push("banana"); // 在末尾追加一個(gè)數(shù)組元素
console.log(arr1.length); // 重新打印了數(shù)組長度,發(fā)現(xiàn)長度改變。說明數(shù)組是可變的
var arrStr = arr1.join("|"); // 把數(shù)組中的元素,通過指定的字符串連接成一個(gè)字符串返回
console.log(arrStr);
var arr2 = [4,2,3,1]
console.log(arr2.sort()); // 默認(rèn)升序排序
// 注意,再循環(huán)中,注意數(shù)組長度的變化
arr1.forEach(function(val,index){
console.log(val,index);
})
</script>
2.2,基本方法
var list = [1, 2, 3]; // 創(chuàng)建數(shù)組并賦值。
console.log(list.indexOf(1));// 返回1這個(gè)元素在數(shù)組中第一次出現(xiàn)的下標(biāo),如果沒有返回-1
list[1] = 22; // 修改指定下標(biāo)的值
list.push(4,5);// 向數(shù)組末尾追加元素,元素?cái)?shù)量不定。返回新的長度
list.unshift(-1,0) // 向數(shù)組頭部添加元素,元素?cái)?shù)量不定。返回新的長度
list.pop(); // 刪除最后一位元素,返回被刪除的元素
list.shift(); // 刪除第一位元素,返回被刪除的元素
list.splice(1); // 從指定下標(biāo)開始刪除,刪到最后。返回刪除的元素?cái)?shù)組
list.splice(1,2); // 刪除從下標(biāo)1開始,長度為2的元素。返回刪除的元素?cái)?shù)組
list.splice(1,1,33,44); // 刪除從下標(biāo)1開始,長度為1的元素,并在刪除的位置上添加兩個(gè)元素,添加的元素個(gè)數(shù)不定
var str = list.join(''); // 把數(shù)組按照指定的間隔符,拼成一個(gè)字符串
var list3 = [11,22,33];
var newlist = list.concat(list3); // 合并數(shù)組,注意合并數(shù)組返回的是新的數(shù)組對(duì)象,對(duì)原對(duì)象不做修改
// console.log(list2)
// console.log(str);
console.log(list);
三,Date對(duì)象
用于處理日期和時(shí)間
使用自UTC(Coordinated Universal Time,國際協(xié)調(diào)時(shí)間)1970年1月1日0時(shí)開始經(jīng)過的毫秒數(shù)來保存日期
創(chuàng)建Date對(duì)象
語法
new Date() // 不帶參數(shù) 當(dāng)前日期
new Date(dateString) // 帶參數(shù),指定日期
示例
<script>
var today = new Date();
var sdate = new Date("July 15,2020,10:07:42");
console.log(today); // 返回當(dāng)前日期和時(shí)間
console.log(sdate); // 返回指定日期和時(shí)間
</script>
3.1,常用方法
方 法 | 說 明 |
---|---|
getDate() | 返回 Date 對(duì)象的一個(gè)月中的每一天,其值介于1~31之間 |
getDay() | 返回 Date 對(duì)象的星期中的每一天,其值介于0~6之間 |
getHours() | 返回 Date 對(duì)象的小時(shí)數(shù),其值介于0~23之間 |
getMinutes() | 返回 Date 對(duì)象的分鐘數(shù),其值介于0~59之間 |
getSeconds() | 返回 Date 對(duì)象的秒數(shù),其值介于0~59之間 |
getMonth() | 返回 Date 對(duì)象的月份,其值介于0~11之間 |
getFullYear() | 返回 Date 對(duì)象的年份,其值為4位數(shù) |
getTime() | 返回自某一時(shí)刻(1970年1月1日)以來的毫秒數(shù) |
<script>
var mydate = new Date();
console.log("年:",mydate.getFullYear());
console.log("月:",mydate.getMonth()+1); // 需要自己+1
console.log("日:",mydate.getDate());
console.log("時(shí):",mydate.getHours());
console.log("分:",mydate.getMinutes());
console.log("秒:",mydate.getSeconds());
console.log("周:",mydate.getDay()); // 周日是0
console.log("時(shí)間戳:",mydate.getTime());
</script>
3.2,小案例
<body>
<div id="datediv"></div>
<div>
<input type="button" value="開始" onclick="startLock()">
<input type="button" id="zanting" value="暫停">
</div>
</body>
<script>
var interIndex;
function startLock(){
interIndex = setInterval("showDate()",1000)
}
document.getElementById("zanting").addEventListener('click',function(){
clearInterval(interIndex)
})
function showDate(){
var mydate = new Date();
var year = mydate.getFullYear()
var month = mydate.getMonth()+1
var day = mydate.getDate()
var hour = mydate.getHours()
var minute = mydate.getMinutes()
var second = mydate.getSeconds()
document.getElementById("datediv").innerHTML=year+"年" +month+"月" +day+"日"+hour+":"+minute+":"+second
}
</script>
js示例
四,Math對(duì)象
提供與數(shù)學(xué)相關(guān)的功能
文章來源:http://www.zghlxwxcb.cn/news/detail-695425.html
常用方法文章來源地址http://www.zghlxwxcb.cn/news/detail-695425.html
方法 | 說 明 | 示例 |
---|---|---|
ceil() | 對(duì)參數(shù)進(jìn)行上舍入 | Math.ceil(25.5);返回26 ---- Math.ceil(-25.5);返回-25 |
floor() | 對(duì)參數(shù)進(jìn)行下舍入 | Math.floor(25.5);返回25 ---- Math.floor(-25.5);返回-26 |
round() | 把參數(shù)四舍五入為最接近的數(shù) | Math.round(25.5);返回26 ---- Math.round(-25.5);返回-26 |
random() | 返回0~1之間的隨機(jī)數(shù) | Math.random(); ---- 例如:0.6273608814137365 |
方法 | forEach() | 遍歷數(shù)組,forEach()方法不會(huì)直接修改原始數(shù)組,但是回調(diào)函數(shù)可能會(huì)修改 |
到了這里,關(guān)于JavaScript(內(nèi)置對(duì)象)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!