内置对象,ES6

本文介绍了JavaScript中的Math对象和Date对象的常用方法,如取整、随机数生成、时间戳获取等,并展示了如何使用计时器方法setInterval。此外,还探讨了ES6的新特性,包括let关键字声明的块级作用域和箭头函数的使用。示例中展示了箭头函数在对象方法中的应用及其与普通函数中this指向的区别。
摘要由CSDN通过智能技术生成

内置对象

Math 
        var num = 3.79
        var result = Math.floor(num)			// 向下取整

        // [1~10] 的随机数
        var num = Math.floor(Math.random()*10+1)
        
Math.abs(num)  // 绝对值
Math.sqrt(num)
Math.pow(2,4)
Date
        var d = new Date()
        var year = d.getFullYear()
        var month = d.getMonth()  // 函数 是将 1月份 当 0开始的
        var date = d.getDate()
        var weekday = d.getDay()  //  返回的是 星期几

        var hours = d.getHours()
        var minutes = d.getMinutes()
        var seconds = d.getSeconds()
        console.log(hours + ":" + minutes + ":" + seconds)
        
        console.log(d.getTime())       // 时间戳

计时器方法 setInterval

        // setInterval(function(){},ms)
        // 每隔一定的毫秒数,去调用一次这个函数。

// 每隔一秒(1000ms),输出一下 当前时间
        setInterval(function(){
            var t = new Date()
            var t_hours = t.getHours()
            var t_minutes = t.getMinutes()
            var t_seconds = t.getSeconds()

            console.log(t_hours + ":" + t_minutes + ":" + t_seconds)
        },1000)

ES6 语法特性

 let is a keyword that is used to declare a block scoped variable。  配合 { } 使用, 提供 块级作用域。

给 内置对象 添加方法:

        var test=[1,2];
        Array.prototype.asy=function(){
            console.log("hello")  
        }
        test.asy()

函数进阶

1.参数可以给默认值
2. 箭头函数

        // 函数表达式
        const fun = function(n,m){
            return n+m
        }
        let result = fun(10,20)
        console.log(result)
  ----------------------------------------
        // 箭头函数:去掉 function 关键字,  在 参数表 和 {} 之间 加上 =>
        const hanshu = (x=10,y=20) => {
            console.log(x,y)
            console.log("这是一个箭头函数")
            
        }
        hanshu(2)
        const cat = {
            name:"miaomiao",
            sayName:function(){
                setInterval(()=>{
                    console.log(this.name)
                    
                },1000)
            }
        }
        cat.sayName()

        // 箭头函数 和普通函数 的 this 指向 不同
        // 普通函数 this 指向 调用该函数的 对象
        // 箭头函数: 在哪里定义,this 就 指向 谁。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值