JS(8)内置对象

1.内置对象

-- JS中的对象分为3种:自定义对象,内置对象,浏览器对象
-- 前面两种都是JS基础内容,属于ECMAScript,浏览器对象属于JS独有的
-- JS提供了多个内置属性:Math,Date,Array,String等

2.Math对象

属性名、方法名 功能
Math.PI 圆周率
Math.floor() 向下取整
Math.ceil() 向上取整
Math.round() 四舍五入 , .5向大
Math.abs() 绝对值
Math.max() 最大值
Math.min() 最小值
Math.min() 最小值
Math.random() 获取范围在[0,1]内的随机数
2.1.获取指定范围内的随机数
function getRandom(min,max){
	return Math.floor(Math.random() * (max -min +1))+min
}

3.日期对象(Date对象)

Date对象和Math对象不一样,Date是一个构造函数,所以按时需要实例化后才能使用其中的具体方法和属性,Date实例用来处理日前和时间。

3.1.使用Date实例化日期对象

-- 1.获取当前时间必须实例化
	let now = new Date()
-- 2.获取指定时间的日期对象
	let funture = new Date('2020/1/1')
注意:如果创建实例时并未传入参数,则得到日期对象是当前时间对象的日期对象

3.2.使用Date实例的方法和属性

属性名、方法名 说明 使用
getFullYear() 获取当年 now.getFullYear()
getMonth() 获取月 now.getMonth()
getDate() 当天日期 now.getDate()
getDay() 星期几(周日0到周六6) now.getDay()
getHours() 小时 now.getHours()
getMinutes() 分钟 now.getMinutes()
getSeconds() 秒钟 now.getSeconds()

3.3.通过Date实例获取总毫秒数

3.3.1.总毫米数的含义:
	基于1970年1月1日(世界标准时间)起的毫米数
3.3.2.获取总毫秒数:
-- 实例化Date对象
	let now = new Date()
-- 1. 用于获取对象的原始值
console.log(now.valuesOf())
console,log(now.getTime())
-- 简单写
	let now = +new Date()
-- H5新增写法:
	let now = Date.now()
发布了37 篇原创文章 · 获赞 53 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_41523392/article/details/104024146