5.1 概念
js里的一种数据类型,无序的数据的集合
5.2 对象使用
语法:let 对象名={ }
对象由属性和方法组成
属性:名词
方法:动词
let 对象名={
属性名:属性值,
方法名:函数
}
查:对象名.属性 或 对象名['属性名']
增:对象名.新属性名=新属性值
删:delete 对象名.属性名
改:对象名.属性名=新属性值
对象中的方法
5.3 遍历对象
let obj={
...
}
for(let k in obj){
console.log(obj[k])
}
// 遍历对象 for in let obj = { uname: 'pink老师', age: 18, gender: '男' } for (let k in obj) { console.log(obj[k]) }
5.4 内置对象
Math
random:生成 [0-1) 之间的随机数
ceil:向上取整
floor:向下取整
max:找最大数
min:找最小数
pow:幂运算
abs:绝对值
// 内置对象:自带的对象 console.log(Math) // 圆周率 console.log(Math.PI) // 向下取整,变小 console.log(Math.floor(4.9)) //4 // 向上取整,变大,只针对小数 3.0向上取整还是3.0 console.log(Math.ceil(4.1)) //5 console.log(Math.ceil(4.0)) //4 // 四舍五入 console.log(Math.round(-3.4)) // -3 console.log(Math.round(-3.9)) // -4 // 绝对值 console.log(Math.abs(-4.1)) //4.1 // 最大值,最小值 console.log(Math.max(3, 4, 6)) //6 console.log(Math.min(3, 4, 6)) //3 console.log(Math.min([3, 4, 6])) //NaN 只能是一组数据,不能是一个数组
5.5 随机函数
Math.random()是返回 [0,1) 之间的随机数
// 随机数,[0,1)之间的随机数
console.log(Math.random())
// 随机整数
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
var num = getRandom(10, 100)
console.log(num)
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
var random = getRandom(1, 50)
for (let i = 0; i < 10; i++) {
let num = +prompt('请输入1-50之间的整数:')
if (num > random) {
alert('猜大了')
} else if (num < random) {
alert('猜小了')
} else {
alert('中奖了')
}
}
5.6 日期对象
Date 对象和 Math 对象不一样,Date是一个构造函数,所以使用时需要实例化后才能使用其中具体方法和属性
Date 实例用来处理日期和时间
-
使用Date实例化日期对象
-
获取当前时间必须实例化:
var now = new Date();
-
获取指定时间的日期对象
var future = new Date('2023/5/1');
注意:如果创建实例时并未传入参数,则得到的日期对象是当前时间对应的日期对象
-
-
使用Date实例的方法和属性
-
通过Date实例获取总毫米数
-
总毫秒数的含义
基于1970年1月1日(世界标准时间)起的毫秒数
-
获取总毫秒数
console.log(new Date()) //中国标准时间 console.log(+new Date()) //毫秒数 1970.01.01-此时此刻的毫秒数 console.log(Date.now()) //毫秒数(h5新增的方法,有兼容性)
-
-
案例1:获取现在的时间,并以官方格式打印在控制台
var date=new Date() var y = date.getFullYear() < 10 ? '0' + date.getFullYear() : date.getFullYear() var m = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1 var d = date.getDate() < 10 ? '0' + date.getDate() : date.getDate() var h = date.getHours() < 10 ? '0' + date.getHours() : date.getHours() var ms = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes() console.log(y + '/' + m + '/' + d + ' ' + h + ':' + ms)
-
案例2:用函数封装倒计时
function getCountDown(t) { var nowDate = +new Date() var newDate = +new Date(t) var time = (newDate - nowDate) / 1000 //1s=1000ms var d = parseInt(time / 60 / 60 / 24) var h = parseInt(time / 60 / 60 % 24) var m = parseInt(time / 60 % 60) var s = parseInt(time % 60) return '倒计时:'+d+'天'+h+'时'+m+'分'+m+'秒' } var time=getCountDown('2024/3/8 20:00:00') console.log(time)
5.7 字符串对象
-
查找字符所在位置的索引-indexOf()
符合条件的第一个元素
var str='hello world' console.log(str.indexOf('l')) //2
-
替换字符串
字符串名.replace(被替换的字符串,新的字符串)
字符串名.replaceAll(被替换的字符串,新的字符串)
var str='hello world' console.log(str.replace('l','哈哈')) //he哈哈lo world console.log(str.replaceAll('l','哈哈')) //he哈哈lo world
-
substr(开始截取下来的索引,截取元素的个数)-------已废弃
-
splice() 方法提取字符串的一部分,并将其作为新字符串返回,而不修改原始字符串
-
concat() :返回新字符串,不改变原来的字符串
-
charAt(索引数值) 根据索引返回字符
var str='hello world' console.log(str.substr(0,3)) //hel console.log(str.concat('你好世界')) //hello world你好世界 console.log(str.charAt(0)) //h