内置对象
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 就 指向 谁。