1.Number对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var num = 10;
var num2 = new Number(5);
console.log(num2);
// Number对象的方法
// Number() parseInt() parseFloat()
// tofixed() 方法 将数值转换为字符串,四舍五入保留小数点后几位
var num3 = 3.1415926;
console.log(num3.toFixed(3)); //3.142
// toString() 进行进制转换,转换后变为字符串形式
</script>
</body>
</html>
2.Boolean对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var bool = true;
var bool1 = new Boolean(false);
console.log(bool1);
</script>
</body>
</html>
3.日期对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// Date
// 实例化日期对象
// 获取当前日期
var date = new Date();
console.log(date);
// 获取指定日期对象
var date1 = new Date('2002/9/7');
console.log(date1);
var date2 = new Date('2002-09-07 00:00:00');
console.log(date2);
// 日期对象的方法
// getFullYear() 获取当前年
var year = date.getFullYear();
console.log(year);
// getMonth() 获取当前月份,比当前月份少1
var month = date.getMonth() + 1;
console.log(month);
// getDate() 获取当前日
var day = date.getDate();
console.log(day);
// getDay() 获取周几
var week = date.getDay();
console.log(week);
// getHours() 获取小时
var hour = date.getHours();
console.log(hour);
// getMinutes() 获取分钟
var min = date.getMinutes();
console.log(min);
// getSeconds() 获取秒
var second = date.getSeconds();
console.log(second);
var arr = ['日', '一', '二', '三', '四', '五', '六'];
var trueWeek = arr[week];
function fun(a) {
if (a < 10) {
a = '0' + a;
}
return a;
}
hour = fun(hour);
min = fun(min);
second = fun(second);
document.write('当前时间为:' + year + '年' + month + '月' + day + '日 ' + '星期' + trueWeek + ' ' + hour + ':' + min + ':' + second)
</script>
</body>
</html>
4.时间戳 getTime() valueOf()
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// var now = new Date();
// var birthday = new Date('2002-09-07 00:00:00');
// var year = now.valueOf() - birthday.valueOf();
// var second = parseInt(year / 1000 % 60);
// var minutes = parseInt(year / 1000 / 60 % 60);
// var hours = parseInt(year / 1000 / 60 / 60 % 24);
// var day = parseInt(year / 1000 / 60 / 60 / 24);
// console.log('已经活了:' + day + '天' + hours + '时' + minutes + '分' + second + '秒');
var now = new Date();
var guoqing = new Date('2023-10-01 00:00:00');
var cha = guoqing.valueOf() - now.valueOf();
var second = parseInt(cha / 1000 % 60);
var minutes = parseInt(cha / 1000 / 60 % 60);
var hours = parseInt(cha / 1000 / 60 / 60 % 24);
var day = parseInt(cha / 1000 / 60 / 60 / 24);
console.log('国庆倒计时:' + day + '天' + hours + '时' + minutes + '分' + second + '秒');
</script>
</body>
</html>
5.Math对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 圆周率
var Pi = Math.PI;
console.log(Pi);
// 求最大值和最小值
var max = Math.max(2, 3, 6, 9, 6, 3, 022, 020, 10, 23, 33);
console.log(max);
var min = Math.min(3, 6, 9, 2, 01, 0, 25, 9, 21, 20);
console.log(min);
// var arr = [3, 6, 9, 3, 35, 0, 23, 02, 22];
// console.log(Math.max(arr));
// 绝对值
var num = -1.32;
console.log(Math.abs(num));
// 求次幂
console.log(Math.pow(2, 10));
// 向下取整
var num2 = 123.145;
console.log(Math.floor(num2));
console.log(Math.floor(11.2));
// 向上取整
console.log(Math.ceil(num2));
// 四舍五入
console.log(Math.round(num2));
// 随机数
console.log(Math.random()); //0-1 之间取随机数
console.log(parseInt(Math.random() * 10) + 1); //0-10 之间取随机数
console.log(parseInt(Math.random() * 51 + 50)); //0-50之间的随机数
// 取一个区间内随机数公式
// Math.random()*(max-min+1)+min
</script>
</body>
</html>
6.全局对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// isFinite() 判断传入的参数是否是一个有限值
// Infinity 无穷大
// eval()方法可以计算字符串
var str = '1+2+3+6';
console.log(eval(str));
</script>
</body>
</html>
7.js数据类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// 基本数据类型
// string number boolean undefined null
// 引用数据类型
// object function Date Array.....
// 引用数据类型的访问、增删改
// 数组
// 增加
var arr = [1, 2, 3];
arr[3] = 4;
console.log(arr);
// 修改
arr[2] = 0;
console.log(arr);
// 清空数组
arr.splice(0, arr.length)
arr.length = 0;
arr = [];
// 对象
var obj = {
name: '张三',
age: 20,
}
// 访问对象属性
console.log(obj.name);
console.log(obj['name']);
// 添加属性
obj.gender = '男';
obj['jia'] = '郑州';
console.log(obj);
// 修改
obj.name = '里斯';
console.log(obj);
// 删除
delete obj.jia;
console.log(obj);
</script>
</body>
</html>
8.instanceof检测引用数据类型,开发人员必须明确的确定属于哪种数据类型
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
// typeof返回值:string number undefined boolean object function
var obj = {};
var arr = [];
function fun() {
}
var str = new String('abc');
console.log(typeof obj);
console.log(typeof arr);
console.log(typeof fun);
console.log(typeof str);
// instanceof() 检测引用数据类型,instanceof方法要求开发者明确地确认对象为某种特定类型
console.log(arr instanceof Object);
console.log(arr instanceof Array);
console.log(fun instanceof Object);
</script>
</body>
</html>