1.Boolean对象:
Boolean对象是JavaScript的一个内置对象,用来表示逻辑值:true或false。
Boolean对象有以下特点和用法:
Boolean对象可以通过使用构造函数来创建新的Boolean对象。例如:
let bool = new Boolean(true);
-
Boolean对象的值可以是true或false,也可以是其他Js数据类型的值。当Boolean对象的值为false时,它表示逻辑假;当值为true时,表示逻辑真。
-
Boolean对象可以进行逻辑运算,如与(&&)、或(||)、非(!)。
当需要将Boolean对象转换为原始的布尔值时,可以使用Boolean对象的valueOf()方法或toString()方法。let a = true; let b = false; let result = a && b; // false
let bool = new Boolean(true); console.log(bool.valueOf()); // true console.log(bool.toString()); // "true"
总结:Boolean对象是用来表示逻辑值的Js内置对象,可以通过构造函数创建新的Boolean对象。Boolean对象的值可以是true或false,可以进行逻辑运算,并且可以通过valueOf()方法或toString()方法转换为原始的布尔值.Number
2.Number:
-
Number对象拥有一些常用的方法,如toFixed(),toPrecision(),toString()等,可以用于对数字进行格式化、精确度控制、转化为字符串等操作。
-
Number对象还拥有一些常用的属性,如MAX_VALUE、MIN_VALUE、NaN、POSITIVE_INFINITY、NEGATIVE_INFINITY等,可以用于获取部分特殊值。
-
console.log("数字的最大值:"+Number.MAX_VALUE);
console.log("数字的最小值:"+Number.MIN_VALUE);
console.log("负无穷大:"+Number.NEGATIVE_INFINITY);
console.log("正无穷大:"+Number.POSITIVE_INFINITY); -
//声明一个数组
let a=1123;
let b=Number(1123);
let c=new Number(2345);
console.log("2进制:"+b.toString(2));
console.log("8进制:"+b.toString(8));
console.log("16进制:"+b.toString(16));
console.log("36进制:"+b.toString(36));
console.log("本地转换:"+b.toLocaleString()); -
//Number 四舍五入为指定小数位数的数字
console.log("指定小数:"+c.toFixed(2));
console.log("指数:"+c.toExponential(10));
console.log("默认值:"+c.valueOf());
Math:
- Math.abs():返回一个数的绝对值。
- Math.ceil():向上取整,返回大于等于参数的最小整数。
- Math.floor():向下取整,返回小于等于参数的最大整数。
- Math.round():四舍五入,返回参数的最接近的整数。
- Math.max():返回一组数中的最大值。
- Math.min():返回一组数中的最小值。
- Math.random():返回一个介于0和1之间的随机数。
- Math.sqrt():返回一个数的平方根。
- Math.pow():返回一个数的指定次幂。
- Math.PI:圆周率的近似值。
- console.log("自然底数e" + Math.E);
console.log("圆周率:" + Math.PI);
console.log("返回数的绝对值:" + Math.abs(1));
console.log("返回数的反余弦值:" + Math.acos(0.9));
console.log("反正切值:" + Math.atan(2));
console.log("返回从x轴到点(x,y)的角度:" + Math.atan2(10.5));
console.log("余弦:" + Math.cos(0.9));
console.log("正弦:" + Math.sin(0.5));
console.log("正切:" + Math.tan(9));
console.log("上舍入" + Math.ceil(123.1));
console.log("下舍入" + Math.floor(123.9));
console.log("四舍五入" + Math.round(123.4));
console.log("返回 e 的指数" + Math.exp(123));
console.log("e的自然对数:" + Math.log(123));
console.log("最大值" + Math.max(12, 15));
console.log("最小值" + Math.min(12, 18));
console.log("x的y次幂:"+Math.pow(2,6));
console.log("0~1之间的随机数"+Math.random());
Date:
Date对象用来处理日期和时间的操作,它是JavaScript中处理日期和时间的内置对象。
-
创建Date对象: 使用
new Date()
可以创建一个表示当前时间的Date对象。 使用new Date(year, month, day, hour, minute, second, millisecond)
可以创建一个指定日期和时间的Date对象。 使用new Date(milliseconds)
可以创建一个指定毫秒数的Date对象。 -
获取日期和时间信息: Date对象有很多方法可以用来获取不同粒度的日期和时间信息,如
getDate()
获取日期,getMonth()
获取月份,getFullYear()
获取年份,getHours()
获取小时等等。 这些方法返回的值都是整数,表示相应的日期和时间信息。 -
设置日期和时间信息: Date对象也有对应的设置方法,如
setDate()
设置日期,setMonth()
设置月份,setFullYear()
设置年份,setHours()
设置小时等等。 这些方法可以用来修改Date对象的日期和时间信息。 -
计算日期和时间: Date对象还有一些计算日期和时间的方法,如
getTime()
返回一个表示从1970年1月1日00:00:00 UTC到当前Date对象时间的毫秒数。 使用Date.now()
可以快速获取当前时间的毫秒表示。 - //1.创建对象
let now = new Date();
document.write('<h2>' + now + '</h2>');
document.write('<h2>返回当日的日期和时间:' + Date() + '</h2>');
document.write('<h2>一个月中的某一天' + now.getDate() + '</h2>');
document.write('<h2>一周中的某一天' + now.getDay() + '</h2>');
document.write('<h2>月' + now.getMonth() + '</h2>');
document.write('<h2>四位数的年' + now.getFullYear() + '</h2>');
document.write('<h2>时' + now.getHours() + '</h2>');
document.write('<h2>分' + now.getMinutes() + '</h2>');
document.write('<h2>秒' + now.getSeconds() + '</h2>');
//2024年7月2 8:50:30
document.write('<h2>' +
now.getFullYear() + "年" +
(now.getMonth() + 1) + "月" +
now.getDate() + "日" +
now.getHours() + ":" +
now.getMinutes() + ":" +
now.getSeconds() +
'</h2>')
document.write('<h2>毫秒' + now.getMilliseconds() + '</h2>');
document.write('<h2>1970年1月1日至今的毫秒数' + now.getTime() + '</h2>');
document.write('<h2>格林威治时间和本地时间之间差' + now.getTimezoneOffset() + '</h2>');
document.write('<h2>指定时间的毫秒数' + Date.parse('Jul 02 2024') + '</h2>');
document.write('<h2>-------------------------</h2>');
//设置时间
now.setFullYear(2025)
document.write('<h2> Date 对象转换为字符串'+now.toString()+ '</h2>');
document.write('<h2 时间部分转为字符串>'+now.toTimeString()+'</h2>');