javascript的math对象
math方法
sqrt:开方
ab:绝对值
PI:π
pow:x的y次方
round:取整
floor:向下
ceil:向上
max:最大数
min:最小值
random:随机数
document.write(Math.sqrt(16)+’
’); //开方 4
document.write(Math.abs(-1)+’
’); //绝对值 1
document.write(Math.PI*2+’
’); //π 6.28…
document.write(Math.pow(2,3)+’
’); //x的y次方 8
document.write(Math.round(3.6)+’
’); //取整,四舍五入 4
document.write(Math.floor(3.2)+’
’); //向下取整 3
document.write(Math.ceil(3.2)+’
’); //向上取整 4
document.write(Math.max(1,34,23)+’
’); //最大值 34
document.write(Math.min(1,34,23)+’
’); //最小值 1
document.write(Math.random()*100+’
’); //0-100随机数 23
javascript的日期对象
data方法
getFullYear:获取年
getMonth:获取月
getDate:获取日
getDay:获取周几
getHours:获取小时
getMINutes:获取分钟
getSeconds:获取秒数
Date.now:时间戳
- 时间戳:格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数
var today = new Date();
document.write(today+’
’);
//Mon Dec 31 2018 10:22:51 GMT+0800 (中国标准时间)
var year = today.getFullYear();
document.write(year+’
’); //2018
var month = today.getMonth()+1;
document.write(month+’
’); //12
var day = today.getDay();
document.write(day+’
’); //1周一
var day1 = today.getDate();
document.write(day1+’
’); //31三十一号
var hour = today.getHours();
document.write(hour+’
’); //10(时)
var minute = today.getMinutes();
document.write(minute+’
’); //34(分)
var second = today.getSeconds();
document.write(second+’
’); //10(秒)
document.write(year+’/’+month+’/’+day1+’/’+hour+’:’+minute+’:’+second)
// 2018/12/31/10:37:14
document.write(Date.now()) //1546224045934(ms)
javascript的函数
- 函数是通常把一系列重复使用的操作封装成一个方法,方便调用
- 定义一个函数
- function funName(){}
- 函数分类
- 有名函数
- 匿名函数
有名函数
function add(参数) {
alert("我被调用了!")
}
add() //调用
匿名函数
var box = document.getElementsByTagName("div")[0];
box.onclick = function () {
alert("434")
}
带参数传参
function ase(a) {
alert(a)
}
ase(222);
不定长传参(以下两种都不报错)
1.function qwe(a,b) {
console.log(a,b)
}
qwe(1) //1传给a,另一个未被定义
eg;
function asdf(a,s,d) {
return a
}
var c = asdf(2);
document.write©; //2
2.function deq(a) {
console.log(a)
}
qwe(1,2,3)
eg:
function asdf(a) {
return a
}
var b = asdf(2,23,24);
document.write(b); //2
函数表达式
!function () {
document.write("w"+"<br>")
}(); //w
+function () {
document.write("we"+"<br>")
}(); //we
-function () {
document.write("wer"+"<br>")
}(); //wer
(function () {
document.write("wert"+"<br>")
})(); //wert
(function () {
document.write("wertu"+"<br>")
}()); //wertu
作用域
var 是定义局部变量
var a = 100;
function func() {
var a = 200;
alert(a);
}
alert(a);
func();
alert(a) //100,200,100
不用var是定义全局变量
var a = 100;
function func() {
a = 200;
alert(a);
}
alert(a);
func();
alert(a) //100,200,200
javascript的定时器
- 设置定时器:setTimeout
- 清除定时器:clearTimeout
- 设置定时器:setInterval
- 清除定时器:clearInterval
清除定时器的时候要给定时器加个名字