一、命名規(guī)范
駝峰命名法
匈牙利命名法
oBtn object 一個按鈕
aBtn array 一組按鈕
sName string 字符串名字
iAge int 整數(shù)年齡
fPrice float 小數(shù)價格
二话告、流程控制語句
if
if else
if else if
switch
switch(變量){
case item1:
coding
break;
case item2:
coding
break;
case item3:
coding
break;
default:
coding
break;
}
三兼搏、返回值
return
function 函數(shù)名(參數(shù)){
coding
return ;
}
調(diào)用某個函數(shù),可以返回一個東西沙郭。
return有特點
如果不寫return佛呻,默認返回undefined
如果寫了return,但是沒寫值病线。依然返回undefined
return后面的代碼不執(zhí)行
=================================================
return的作用:
1.返回東西
2.阻斷程序執(zhí)行
function show(){
oDiv.style.display = 'block';
}
show()
function sum(a,b){
return a+b;
}
var result = sum(12,5);
四吓著、隨機數(shù)
隨機數(shù)
Math.random()
從0-1的隨機小數(shù)
會出現(xiàn)0,但是絕對不會出現(xiàn)1
獲取從n-m的隨機整數(shù)
function rnd(n,m){
return parseInt(Math.random()*(m-n)+n);
}
rnd(10,20);
五送挑、運算符
算數(shù)運算符
+ - * / %(模绑莺、取余)
%
10%3 1
2%2 0
隔行變色
0 %2 0
1 %2 1
2 %2 0
賦值運算符
=
+= -= *= /= %=
比較運算符
> < >= <=
== !=(不等)
===(全等) !==(不全等)
邏輯運算符
&& (與, 并且) 都滿足
|| (或, 或者) 滿足一個
! (非, 取反) 相反的結(jié)果
六、定時器
setInterval 隔一段時間執(zhí)行一次
var timer = setInterval(function(){
},ms); ms毫秒
setTimeout 過一段時間執(zhí)行一次惕耕,只有一次纺裁。
var timer = setTimeout(function(){
},ms);
注意:開定時器之前,先清除
清除定時器
clearInterval(timer);
clearTimeout(timer);
七司澎、日期對象
var oDate = new Date();
獲取年
oDate.getFullYear();
獲取月
oDate.getMonth();
從0開始
獲取日
oDate.getDate();
獲取時
oDate.getHours();
獲取分
oDate.getMinutes();
獲取秒
oDate.getSeconds();
八欺缘、設置日期時間
設置年
oDate.setFullYear(y,m,d); 設置年月日
oDate.setFullYear(y,m); 設置年月
oDate.setFullYear(y); 設置年
設置月
oDate.setMonth(m,d); 設置月日
oDate.setMonth(m); 設置月
設置日
oDate.setDate(d); 設置日
不能設置星期
設置時
oDate.setHours(h,m,s,ms); 設置時分秒毫秒
oDate.setHours(h,m,s); 設置時分秒
oDate.setHours(h,m); 設置時分
oDate.setHours(h); 設置時
設置分
oDate.setMinutes(m,s,ms); 設置分秒毫秒
oDate.setMinutes(m,s); 設置分秒
oDate.setMinutes(m); 設置分
設置秒
oDate.setSeconds(s,ms); 設置秒毫秒
oDate.setSeconds(s); 設置秒
設置毫秒
oDate.setMilliseconds(ms); 設置毫秒
九、倒計時
倒計時
獲取目標時間挤安,時間戳
時間差 = 目標時間戳-當前時間戳
設置時間戳
oDate.setTime();
時間戳 格林威治時間(1970-1-1 00:00:00.000)
從1970年1月1日0點0分0秒0毫秒開始谚殊,到現(xiàn)在所經(jīng)歷的毫秒數(shù)