JavaScript包装类
1166 字
6 分钟
JavaScript包装类
本文主要学习JavaScript包装类和Math、Date对象
包装类
什么是包装类
-
Number()、String()和Boolean()分别是数字、字符串、 布尔值的“包装类”
-
很多编程语言都有“包装类”的设计,包装类的目的就是为 了让基本类型值可以从它们的构造函数的prototype上获得 方法
var a = new Number(123);var b = new String('柚子177');var c = new Boolean(true);console.log(typeof a); //objectconsole.log(typeof b); //objectconsole.log(typeof c); //objectconsole.log(5 + a); //可以参与运算console.log(b.slice(0, 2));console.log(c && true);var d = 1234;console.log(d.__proto__ == Number.prototype); //truea、b、c是基本类型值么?它们和普通的数字、字符串、布 尔值有什么区别么?
-
Number()、String()和Boolean()的实例都是object类型 ,它们的PrimitiveValue属性存储它们的本身值
-
new出来的基本类型值可以正常参与运算
-
包装类的目的就是为了让基本类型值可以从它们的构造函数 的prototype上获得方法
Math(数学)对象
已经学习过的Math对象的方法
- 幂和开方:Math.pow()、Math.sqrt()
- 向上取整和向下取整:Math.ceil()、Math.floor()
四舍五入Math.round()
-
Math.round()可以将一个数字四舍五入为整数
console.log(Math.round(3.4)); // 3console.log(Math.round(3.5)); // 4console.log(Math.round(3.98)); // 4console.log(Math.round(3.49)); // 3 -
四舍五入到小数点后某位
function roundTo(num, digits) {const factor = Math.pow(10, digits);return Math.round(num * factor) / factor;}roundTo(3.14159, 2); // 3.14roundTo(3.145, 2); // 3.15roundTo(3.1, 3); // 3.100
Math.max()和Math.min()
-
Math.max()可以得到参数列表的最大值
-
Math.min()可以得到参数列表的最小值
console.log(Math.max(6, 2, 9, 4)); // 9console.log(Math.min(6, 2, 9, 4)); // 2
如何利用Math.max()求数组最大值
-
Math.max()要求参数必须是“罗列出来”,而不能是数组
-
还记得apply方法么?它可以指定函数的上下文,并且以数组的 形式传入“零散值”当做函数的参数
var arr = [3, 6, 9, 2];var max = Math.max.apply(null, arr);console.log(max); // 9
随机数Math.random()
-
Math.random()可以得到0~1之间的小数
-
为了得到[a, b]区间内的整数,可以使用这个公式:
parseInt(Math.random() * (b - a + 1)) + a
Date(日期)对象
- 使用new Date()即可得到当前时间的日期对象,它是object类 型值
- 使用new Date(2020, 11, 1)即可得到指定日期的日期对象, 注意第二个参数表示月份,从0开始算,11表示12月
- 也可以是new Date(‘2026-06-16’)这样的写法
日期对象的常见的方法
| 方法 | 功能 |
|---|---|
| getDate() | 得到日期1~31 |
| getDay() | 得到星期0~6 |
| getMonth() | 得到月份0~11 |
| getFullYear() | 得到年份 |
| getHours() | 得到小时数0~23 |
| getMinutes() | 得到分钟数0~59 |
| getSeconds() | 得到秒数0~59 |
时间戳
- 时间戳表示1970年1月1日零点整距离某时刻的毫秒数
- 通过getTime()方法或者Date.parse()函数可以将日期对象变 为时间戳
- 通过new Date(时间戳)的写法,可以将时间戳变为日期对象
小案例:倒计时小程序
在页面上实时显示距离2027年高考还有多少天、多少时 、多少分、多少秒
<div> 距离2027年高考还有: <span id="days">0</span> 天 <span id="hours">0</span> 时 <span id="minutes">0</span> 分 <span id="seconds">0</span> 秒</div>
<script> // 🎯 目标时间(2027高考) const targetDate = new Date("2027-06-07 00:00:00").getTime();
function updateCountdown() { const now = new Date().getTime(); const diff = targetDate - now;
// 如果已经过了 if (diff <= 0) { document.body.innerHTML = "⏰ 高考时间已到!"; clearInterval(timer); return; }
// 计算时间 const seconds = Math.floor(diff / 1000); const days = Math.floor(seconds / (60 * 60 * 24)); const hours = Math.floor((seconds % (60 * 60 * 24)) / (60 * 60)); const minutes = Math.floor((seconds % (60 * 60)) / 60); const secs = seconds % 60;
// 写入页面 document.getElementById("days").textContent = days; document.getElementById("hours").textContent = hours; document.getElementById("minutes").textContent = minutes; document.getElementById("seconds").textContent = secs; }
// 先执行一次(避免1秒空白) updateCountdown();
// 每秒更新 const timer = setInterval(updateCountdown, 1000);</script><body> <h1>2027年高考倒计时</h1> <h2 id="info"></h2> <script> var info = document.getElementById('info'); function updateCountdown() { // 现在的日期 var nd = new Date(); // 目标的日期,5表示六月 var td = new Date(2027, 5, 7);
// 毫秒差 var diff = td - nd;
// 任务很简单,就是把diff换算为天、小时、分钟、秒 // 换算为多少天,除以一天的总毫秒数,不就是换算为多少天么 var day = parseInt(diff / (1000 * 60 * 60 * 24)); // 零多少小时呢??差的总毫秒数与1天的毫秒数的相除的余数,就是零头的毫秒数 var hours = parseInt((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); // 零多少分钟呢?? var minutes = parseInt((diff % (1000 * 60 * 60)) / (1000 * 60)); // 零多少秒呢?? var seconds = parseInt( ((diff % (1000 * 60 * 60)) % (1000 * 60)) / 1000, ); info.innerText = day + '天' + hours + '时' + minutes + '分' + seconds + '秒'; } updateCountdown(); //// 先执行一次(避免1秒空白) setInterval(updateCountdown, 1000); </script></body>文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
相关文章智能推荐
1
JavaScript函数
前端工程师2026-10-19
2
JavaScript面向对象
前端工程师2026-10-20
3
JavaScript数组
前端工程师2026-10-19
4
JavaScript正则表达式
前端工程师2026-10-20
5
JavaScript基本数据类型
前端工程师2026-10-18
随机文章随机推荐














