本周主要學習內容是js基礎。
JavaScript 是一種具有函數(shù)優(yōu)先的輕量級,解釋型或即時編譯型的高級編程語言。
1.如何定義變量
? ? 使用var 關鍵字定義變量;
? ? 變量命名:
? ? 變量命名必須以字母、下劃線”_”或者”$”為開頭。其他字符可以是字母、_、美元符號或數(shù)字。
? ? 變量名中不允許使用空格和其他標點符號,首個字不能為數(shù)字。
? ? 變量名長度不能超過255個字符。
? ? 變量名區(qū)分大小寫。(javascript是區(qū)分大小寫的語言)
? ? 變量名必須放在同一行中
? ? 不能使用腳本語言中保留的關鍵字、保留字、true、false 和 null 作為標識符。
2.數(shù)據(jù)類型
? ? 基本數(shù)據(jù)類型:number,string,bollean,null,undefinde;
? ? 引用數(shù)據(jù)類型:對象;
3.數(shù)據(jù)類型轉換
? ? 轉為整型:parseInt();
? ? 轉為浮點型:parsfloat(); 保留小數(shù)點
? ? 轉為數(shù)字型:Nunmber(); 可保留小數(shù)點
? ? 轉換為字符串:string();
? ? 轉換為布爾型:Boolea();
? ? 隱式類型轉換:console.log(1+true); //2
4.運算符
? ? 算術運算符:+ - / * % ++ --
? ? 關系運算符:> < >=? <=? ==? ===? !=? !==
? ? 邏輯運算符:&&? ||? !
? ? 賦值運算符:=? +=? -=? *=? /=? %=
? ? 三目運算:運算符?表達式1:表達式2;
5.分支選擇結構
? ? 單分支:if{]
? ? 雙分支:if{} else{}
? ? 多分支:if{} else if{]? else{]
6.循環(huán)
先驗證后循環(huán) while ,for
先循環(huán)后驗證? ? do {} while(條件)
continue;跳出本次循環(huán),繼續(xù)執(zhí)行后面的內容
break;跳出循環(huán)
7.數(shù)組? ?
定義數(shù)組:var arr[,,,,,] ; 可定義稀疏數(shù)組
? ? ? ? ? ? ? ? ? var arr=new Arraay(1,2,3);//括號內只有一個數(shù)字時,表示數(shù)組的長度;不能省略中間值
末尾添加/刪除值:arr.push('d');arr.pop();
首部添加/刪除值:arr.unshift('a'); arr.shift();
任意位置進行添加/刪除:arr.splice(start,delcount,newval);
? ? ? ? arr.splice(1,1) //刪除下標1的元素
? ? ? ? arr.splice(1,0,d) //在下標為1的位置插入元素d
? ? ? ? arr.splice(2) //刪除下標2之后的所有元素
翻轉數(shù)組? arr.reverse();
數(shù)據(jù)排序? arr.sort() ;//按照首字符進行排序
拼接數(shù)組? arr.conact(arr2);? //不會改變原數(shù)組
組合數(shù)據(jù)? arr.join();? ? //'a','b','c'
? ? ? ? ? ? ? ? arr.join('');? ? //'abc'
toString? 轉換為字符串,可用于任何類型數(shù)據(jù)
裁剪數(shù)組:
? ? ? ? arr2=arr.slice(1) //從下標為1的元素開始裁剪 b,c,d,e
? ? ? ? arr2=arr.slice(1,3) //裁剪1到3,不包括結束位置 b c
返回值
? ? push? 返回數(shù)組的長度;
? ? pop? 返回被刪除的值;
? ? splice 返回被刪除值的組成數(shù)組;
? ? reverse 返回翻轉后的數(shù)組;
? ? sort 返回排序好后的數(shù)組;
多維數(shù)組:var arr=[,[,[,,]]]
判斷是否是一個數(shù)組:Array.IsArray(arr);
Indexof()
? ? arr.indexof(3); //存在對應的值,就返回下標;不存在對應的值,就返回-1;
遍歷數(shù)組
? ? for
? ? for(var key? in? arr){console.log(arr[key]);}? //遍歷數(shù)組時不包含無值項,跳過稀疏數(shù)組
? ? for(var item? of? arr){console.log(item]);}? //專用于數(shù)組,遍歷所有的項
8.數(shù)字對象 Math
? ? Math.PI:π的值;
? ? Math.round(n):四舍五入;
? ? Math.floor(n):向下取整;
? ? Math.ceil(n):向上取整;
? ? Math.max(n1,n2,...):最大值;
? ? Math.min(n1,n2,...):最小值;
? ? Math.pow(x,y):求冪;
? ? Math.sqrt(n):求平方根;
? ? Math.abs(n):絕對值;
? ? Math.sign(n):判斷正負數(shù); //正數(shù)返回1,負數(shù)返回-1;0返回0;-0返回-0;
? ? Math.random():隨機數(shù);
? ? ? ? 取任意區(qū)間的數(shù)字:Math.random()*(大-小+1)+??;
9. 時間對象
? ? 獲取系統(tǒng)時間 var? time=new date();
? ? 自定義時間:new? date(年,月,日,時,分,秒,毫秒)
? ? new? date(2000); 當今有一個參數(shù)時,表示毫秒
? ? 獲取年:time.fullyear();
? ? 獲取月:time.month();
? ? 獲取日:time.day();
? ? 獲取時:time.hours();
? ? 獲取分:time.minutes();
? ? 獲取秒:time.seconds();
? ? 獲取星期:time.date();
? ? 獲取毫秒:time.time();
? ? 獲取當前這一秒的毫秒:time.getMillseconds();
10. 字符串對象
? ? var str=‘helloworld’;
? ? str.charAt(0);? //獲取下標為0位置上的字符
? ? str.chartCodeAt(0);? //返回下標為0位置上的字符編碼;
? ? str.fromCharCode(104);? //返回104對應的中字符
? ? str.indexof('0');? //返回指定字符首次出現(xiàn)的位置;
? ? str.indexof('0',4)? //從起始位置4開始搜索0出現(xiàn)的位置
? ? str.lastindexof('0');? //從后向前搜索指定字符出現(xiàn)的首次位置
? ? str.search('l');? ? //查找指定字符首次出現(xiàn)的位置,可以傳入正則表達式,只有一個參數(shù)
? ? str.slice(start,end)? ? //不包含結束
? ? ? ? str.slice();//返回所有字符串
? ? ? ? str.slice(2);//返回從下標為2開始的剩余字符串
? ? ? ? str.slice(2,4);//start開始下標 end結束下標(不包含) 參數(shù)可以為負數(shù)
? ? str.substring(2,4));//參數(shù)不可以為負數(shù)
? ? str.substr(from,length);
? ? str.replace(查找的子串,新的值);
? ? 轉成大寫? str.toUpperCase();str.toLocaleUpperCase();
? ? 轉成小寫? str.toLowerCase();
? ? 拼接字符串:str.concat('你好');
? ? 去除前后空格:str.trim();
? ? console.log(str.split());//返回一個數(shù)組
? ? console.log(str.split(''));//["h", "e", "l", "l", "o", ",", "w", "o", "r", "l", "d", ",", "你", "好"] 把每個字符拆分 組成一個數(shù)組
? ? console.log(str.split(','));//以指定字符拆分
? ? console.log(str.split('o')); //['hell','w','rld']? 不包括拆分的字符