第一周 Web开发入门(中)
四.JavaScript數(shù)組
JavaScript數(shù)組是指將多個數(shù)據(jù)對象編碼存儲、提供一致的數(shù)據(jù)存取方式集合。
數(shù)組元素:是指存儲在數(shù)組中并賦予唯一索引號的數(shù)據(jù)段。
與其他程序語言不通,JavaScript的數(shù)組元素數(shù)據(jù)類型可以不相同。
各元素的數(shù)據(jù)類型可以是任意有效的JavaScript數(shù)據(jù)類型,元素按添加進(jìn)數(shù)組的順序存儲于數(shù)組中。
1.數(shù)組定義
//方法1 var myphones=new array[4]; myphones[0]=1; myphones[1]=2; myphones[2]=3; myphones[3]=4;//方法2 var myphones[1,2,3,4];//方法3 var myphones=new array(1,2,3,4);2.數(shù)組遍歷方法
//方法1 for循環(huán)實現(xiàn) var phones["小米","三星","蘋果","華為"];for(var i=0;i<phones.length;i++) {document.write(myphones[i]);document.write("<br/>"); }//方法2 for in循環(huán) var phones["小米","三星","蘋果","華為"]; for(key in phones) {document.write(myphones[key]); }3.數(shù)組操作----插入、刪除
? ? ?從隊尾插入刪除元素
? ? push() 方法(在數(shù)組結(jié)尾處)向數(shù)組添加一個新的元素;
? ? pop() 方法從數(shù)組結(jié)尾處中刪除最后一個元素。?
? ? ??從頭部插入刪除元素
? ? unshift() 方法(在開頭)向數(shù)組添加新元素
? ? shift() 方法會刪除開頭的元素。?
? ? ??在任意位置插入刪除元素
? ? splice() 方法既可以在數(shù)組的任意位置插入元素,也可以在數(shù)組的任意位置刪除元素。
var mycars=["Saab","Volvo","BWM"]; //刪除元素 var car=mycars.splice(1,2);//從索引的位置刪除兩個元素 document.write("被刪除的元素是:"+car);//打印刪除的元素 document.write("<br/>"); document.write("刪除操作后的新數(shù)組元素是:"+mycars); document.write("<br/>"); //splice 從指定位置插入元素 mycars.splice(1,0,"Volvo","BWM","寶馬","奔馳"); document.write("插入操作后的新數(shù)組元素是:"+mycars);4.數(shù)組操作----排序
? ? sort()方法以字母順序?qū)?shù)組進(jìn)行排序。
? ? reverse()方法反轉(zhuǎn)數(shù)組的元素。
5.數(shù)組操作----合并
? ? var arr=[1,3,5];
? ? var arr1=[2,4,6];
? ? var arr2=[7,8,9];
? ? var newArr=arr.concat(arr1,arr2);//合并三個數(shù)組
6.數(shù)組轉(zhuǎn)字符串
var mycars = ['寶馬','奔馳','奇瑞','標(biāo)致','捷達(dá)']; var cars = mycars.join("|"); // 用|連接的字符串 //數(shù)組轉(zhuǎn)換為字符串 ,分隔 var cars1=mycars.toString(); document.write(cars); document.write("<br/>"); document.write(cars1);? ? 7.對象數(shù)組
<script>function user(name,age,sex);user1=new user("何濤","19","男");user2=new user("張旺","20","男");array1=new array(user1,user2,user3);for(var I=0;i<arr.length;i++){console.log(array1[i].name);} </script>五.javaScript字符串
用于存儲和操作文本。JavaScript字符串引號的零個或多個字符。
1.字符串搜索
? ? -indexOf()方法
? ? ? ?返回字符串中指定文本首次出現(xiàn)的索引(位置);
? ? -lastindexOf()方法
? ? ? ?返回指定文本在字符串中最后一次出現(xiàn)的索引;
? ? 如果未找到文本,則均返回-1
var str = 'abcdeabcde'; //indexof(搜索詞,起始索引位置) 起始索引位置開始,檢索順序 從前往后 console.log(str.indexOf('a')); // 首次出現(xiàn)的位置 返回0 console.log(str.indexOf('a', 3)); // 返回5 console.log(str.indexOf('bc')); // 返回1 //lastIndexOf(搜索詞,起始索引位置) 檢索順序 從后往前 var str = 'abcdeabcde'; console.log(str.lastIndexOf('a')); // 返回5 console.log(str.lastIndexOf('a', 3)); // 返回0,從索引3的位 置往前檢索? ? ? -search()方法
? ? ? ? ?用于檢索字符串中指定的子字符串,或檢索與正則表達(dá)式相匹配的子字符串。返回相匹配的String對象起始位置。
? ? ? -match()方法
? ? ? ? ?返回一個數(shù)組,其中存放了與他找到匹配文本有關(guān)的信息。
var str = 'abcDEF'; //search(搜索詞) search(正則表達(dá)式) 檢索搜索詞子字符串 返回子字符串在串 的位置 console.log(str.search('c')); //返回2 console.log(str.search('d')); //無匹配,返回-1 console.log(str.search(/d/i)); //返回3,正則語法/i表示忽視大小寫 var str = '1a2b3c4d5e'; //match(搜索詞) match(正則表達(dá)式) 檢索搜索詞子字符串 返回初識索引位置 返回數(shù)組 console.log(str.match('h')); //返回null console.log(str.match('b')); //返回["b", index: 3, input: "1a2b3c4d5e"] console.log(str.match(/d/i)); //返回["d", index: 7, input: "1a2b3c4d5e"]2.字符串替換
? ? replace()方法用另一個值替換在字符串中指定的值
? ? 字符串.replace(正則表達(dá)式/要被替換的字符,要替換成為的子字符串)
//replace()兩個參數(shù) //第一個參數(shù)是字符串或沒有進(jìn)行全局匹配的正則表達(dá)式,只進(jìn) 行一次替換,替換最前面的 document.write(str.replace('a', 'A')); document.write("<br/>"); document.write(str.replace(/a/i, 'A')); //正則表達(dá)式 /i忽略大 小寫 document.write("<br/>"); document.write(str.replace(/a/g, 'A')); // /g全局匹配3.字符串切割
? ? 可以通過split()將字符串轉(zhuǎn)換為數(shù)組。
var str = 'h|e|l|l|o'; console.log(str.split('|')); //返回["h", "e", "l", "l", "o"] console.log(str.split('|', 3)); //返回["h", "e", "l"] console.log(str.split('')); //返回["h", "|", "e", "|", "l", "|", "l", "|", "o"]六.JavaScript函數(shù)
設(shè)計為指定任務(wù)的代碼塊,函數(shù)會在某代碼調(diào)用它被執(zhí)行
Function 函數(shù)名([參數(shù)1])
{
? ? ?[語句組]、[return表達(dá)式];
}
1.函數(shù)聲明三種方式:
? ?-通過函數(shù)名聲明
? ?-通過將匿名函數(shù)賦值給變量
? ?-通過new Function方式聲明
其中前兩種只有在調(diào)用函數(shù)時,才可以執(zhí)行,第三種,直接執(zhí)行,不需要調(diào)用。
//通過函數(shù)名聲明 function fun1(){document.write("我是函數(shù)fun1"); //函數(shù)體document.write('br/'); } fun1();//匿名函數(shù)賦值給變量 var fun2=function(){document.write("我是函數(shù)fun1"); //函數(shù)體document.write('br/'); } fun2();//直接調(diào)用無需執(zhí)行 var fun3=new Function(document.write('我是函數(shù)fun3'));2.函數(shù)調(diào)用
除了使用Function對象的構(gòu)造函數(shù)創(chuàng)建的函數(shù)不需要調(diào)用,其他函數(shù)均需調(diào)用才能執(zhí)行。
(1)傳值調(diào)用
(2)傳址調(diào)用
(3)傳函數(shù)調(diào)用
(4)鏈接調(diào)用、事件觸發(fā)調(diào)用
//傳值調(diào)用 function1 fun1(str){str='你好'; } var a='hello'; fun1(a); document.write(a); //輸出值為hello,形參的值不會改變實參的值//傳址調(diào)用 function2 fun2(person){person.name='何濤'; } var b={name:'李四';} //對象類型 引用類型 fun2(b); //傳地址調(diào)用 document.write(b); //輸出值為何濤//傳函數(shù)調(diào)用 function add(a,b) {return a+b; }function times(a,b) {return a*b; }function operation(a,b,fun) {return fun(a,b); }var test1=operation(3,5,add); document.write(test1);//鏈接調(diào)用 TAT沒看懂3.內(nèi)置函數(shù)----定時器函數(shù)
使用定時器可以為web頁面制作出移動端的景物、變幻的色彩等動畫效果,這些都需要定時器將頁面元素分幀改變其屬性。
JavaScript 定時器有兩個方法:?
??setInterval():?
??setTimeout( ):
<body onload="f1()"> <font id='fTime'></font> <button onclick="stop()">單擊停止時間走動</button><script>var ftime;//類似全部變量var timeId;window.onload=function(){ftime=document.getElementById("fTime");//初始化 timeMove();//調(diào)用函數(shù)}function timeMove(){var date=new Date();//當(dāng)前日期對象 fTime.innerHTML=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();}//往ftime填數(shù)據(jù)timeId=setInterval(timeMove,1000); //每秒調(diào)用一次function stop(){clearInterval(timeId); //}</script> </body>?
?
?
?
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的第一周 Web开发入门(中)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 西工大计算机学院保研人数,陕西多所大学保
- 下一篇: 西电杨宗凯调研计算机学院,杨宗凯调研指导