詳解JavaScript中的函數(shù)、對象
JS中的函數(shù)聲明方式
方式一
function 函數(shù)名(){
函數(shù)體
}

方式二
var 函數(shù)名=function(){
函數(shù)體
}

方式三
var 函數(shù)名=new Function(“函數(shù)體”);

執(zhí)行方式
函數(shù)名();
JS中的對象
類似Java中的一些系統(tǒng)預(yù)設(shè)好的類
日期對象
function testDate(){
var date=new Date();
//本月中的第幾天
document.write(date.getDate()+"<br />");
//本星期的第幾天
document.write(date.getDay()+"<br />");
//返回的月份 0-11
document.write(date.getMonth()+"<br />");
//返回的是1900年到現(xiàn)在年份的差值2018-1900
document.write(date.getYear()+"<br />");
//返回全年2019
document.write(date.getFullYear()+"<br />");
//2019年3月27日 下午5:35:02:返回本地的時間
document.write(date.toLocaleString()+"<br />");
}
testDate();
2、數(shù)學(xué)對象
function testMath(){
//獲得隨機(jī)數(shù)
//隨機(jī)數(shù)范圍是0-1
var ran=Math.random()*1000;
console.log(ran);
//向下取整 756.9714434215177 ==756
console.log(Math.floor(ran));
//向上取整 398.06376470341377==398
console.log(Math.ceil(ran));
//獲得4位隨機(jī)數(shù)--驗證碼
console.log(Math.floor(Math.random()*9000+1000) );
}
testMath();
3、String對象

4、Global對象

5、數(shù)組對象數(shù)組三種聲明方式
//方式一
var arr=new Array();
//方式二 5代表數(shù)組的長度
var arr2=new Array(5);
//方式三
var arr3=new Array("你好",123,new String(),true);
數(shù)組擴(kuò)容

數(shù)組減小

數(shù)組遍歷的兩種方式
function demo4(){
var arr=["bjsxt",123,new Date(),false];
//數(shù)組的遍歷方式一
for(var i =0;i<arr.length;i++){
console.log(arr[i]);
}
//數(shù)組遍歷方式二 i :是代表數(shù)組的下標(biāo)
for(var i in arr ){
console.log(arr[i]);
}
}
demo4();
數(shù)組常用方法:
Push:向數(shù)組的末尾添加一個或更多元素,并返回新的數(shù)組。
Pop:刪除數(shù)組最后一個元素
Shift:刪除數(shù)組第一個元素
Unshift:向數(shù)組的開頭添加一個或更多元素,并返回新的數(shù)組。
Splice: 刪除的含義 開始刪除的下標(biāo) 刪除的個數(shù)
function demo5(){
var arr=["bjsxt",123,new Date(),false];
console.log(arr);
//var le= arr.push("我們");
// var le= arr.pop();
//arr.shift();
//arr.unshift("sxt");
//arr.splice(1,2);
//添加的含義 添加的下標(biāo) 0:刪除元素的個數(shù) 添加的元素
arr.splice(1,0,"你好");
console.log(arr);
}
demo5();
以上所述是小編給大家介紹的JavaScript中的函數(shù)、對象詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
THREE.JS入門教程(4)創(chuàng)建粒子系統(tǒng)
Three.js是一個偉大的開源WebGL庫,WebGL允許JavaScript操作GPU,在瀏覽器端實現(xiàn)真正意義的3D本文將介紹創(chuàng)建一個粒子系統(tǒng)/風(fēng)格/引入物理等等,感興趣的朋友可以了解下哦,希望本文對你有所幫助2013-01-01
JavaScript Break 和 Continue區(qū)別教程
JavaScript Break 和 Continue區(qū)別教程...2007-04-04

