本文转载自:https://blog.csdn.net/xiaogeldx/article/details/85455011

JavaScript的math对象

math方法

  • sqrt:开方

  • abs:绝对值

  • 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.PI2+'
    '); //π 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()+'
    ');//0-1之内的随机数
    document.write(Math.random()
    100+'
    '); //0-100随机数

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; // 从 Date 对象返回月份 (0 ~ 11)
    document.write(month+'
    '); //12
    var day = today.getDay(); //从 Date 对象返回一周中的某一天 (0 ~ 6)
    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);

不定长传参(以下两种都不报错)

function a() {
console.log(arguments);//固定用arguments
}
a(1,3,7,'xiaoge');
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20190331165440281.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW9nZWxkeA==,size_16,color_FFFFFF,t_70)
2.

function b(a,b) {
console.log(arguments);
console.log(a,b)
}
b(1);
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20190331170301279.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW9nZWxkeA==,size_16,color_FFFFFF,t_70)
3.

function b(a,b) {
console.log(arguments);
console.log(a,b)
}
b(1,3,7,'xiaoge');
![在这里插入图片描述]( https://img-blog.csdnimg.cn/20190331165940899.png?x-oss-process=image/watermark ,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3hpYW9nZWxkeA==,size_16,color_FFFFFF,t_70)

函数表达式

  • 自执行

    !function () {
    document.write("w"+"
    ")
    }(); //w
    +function () {
    document.write("we"+"
    ")
    }(); //we
    -function () {
    document.write("wer"+"
    ")
    }(); //wer
    (function () {
    document.write("wert"+"
    ")
    })(); //wert
    (function () {
    document.write("wertu"+"
    ")
    }()); //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
posted on 2019-03-31 20:50  小哥东  阅读(313)  评论(0)    收藏  举报