概念
基础类型
number string boolean null undefined
引用类型
数组 函数 对象
对象理解
对象是指一个具体的事物。万物皆对象。
一个具体的事物一般都会有行为和特征。
手机:
行为:打电话 聊微信 玩和平精英 刷新闻
特征:颜色 屏幕分辨率 摄像头像素
对象的行为------js对象的函数
对象的特征------js对象的属性
JavaScript中的对象
JavaScript中的对象可以看做生活中具体事物的抽象。
js对象是属性+函数的集合
属性:可以是基本数据类型,也可以是引用数据类型。
对象分类
1.内建对象
由ES标准定义的对象,在任何ES实现中都可以使用 js提供的对象
Math Date String Number Boolean Function Object
2.宿主对象
主要有浏览器提供的对象。
BOM DOM
3.自定义对象
开发人员自己定义的对象。
如何创建一个对象
1.使用new关键字类创建var hero = new Object();
2.使用字面量var hero1 = {};
2.1新增属性
hero.name = “韩信”;
hero[“age”] = “30”;
2.2使用属性
对象名.属性名
2.3新增方法
对象名.属性名=function(){
函数体
};
如何输出对象属性
格式:document.write(对象名.属性名); //或者 document.write(对象名["属性名"]);
hero.grade = function() {
alert("我是皇后!");
console.log("我是皇后!");
}
hero.grade();
//-------------------------------
hero.grade1 = fun();
function fun() {
alert("我是皇后!");
}
Math方法
Math.random()返回0-1之间的随机数
document.write(Math.random());
返回n-m之间的随机数
parseInt(Math.random()*(m-n)+n); /Math.round(Math.random() * (m-n)+n); /Math.floor(Math.random() * (m - n + 1) + n);
//随机生成1-5之间的数
document.write(parseInt(Math.random() * (5 - 1) + 1));
//5比较难遇
document.write(parseInt(Math.round(Math.random() * (5 - 1) + 1));
Math.round()四舍五入
document.write(Math.round(5.2));
document.write(Math.round(5.5));
Math.max()取最大值;
document.write(Math.max(10, 12, 60));
结果为最大值60
Math.min()取最小值;
document.write(Math.min(10, 12, 60));
结果为最小值10
Math.abs()取绝对值
document.write(Math.abs(-5));
Math.ceil()向上取整
document.write(Math.ceil(5.001));
Math.floor()向下取整
document.write(Math.floor(5.99999));
结果为5,当数值大于0时,结果只需输出小数点前面的数值
document.write(Math.floor(-5.21));
结果为-6,当数值小于0时,就像是在一个X轴上,-5.21向下也就是向后取整
结果为-6,当数值小于0时,
Math.sqrt()开平方
document.write(Math.sqrt(9));
结果为3
Math.PI();弧度
例子
16进制随机数
function random() {
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"];
var str = '#';
for (var i = 0; i < 6; i++) {
// str += arr[Math.round(Math.random() * 15)];
str += arr[Math.round(Math.random() * 15)];
}
document.write(str);
}
random();
生成一个5-16的随机数
function random() {
var result = Math.floor(Math.random() * 12 + 5);
if (result == 17) {
result = 16;
}
return result;
}
document.write(random());
采用向下取整的方式,
Date方法
获取时间后的展示格式
Wed Jan 06 2021 14:38:02 GMT+0800 (中国标准时间)
星期 月 日 年 时分秒 时区
创建日期对象是,可以指定日期。时分秒若不填写,则默认为00:00:00
指定日期的几种方式:
1.new Date(“2021/01/07”)
2.new Date(2020,09,30,15,33,20);
3.new Date(1576800000000);
4.new Date(“2020-12-31 12:30:20”);
在国外,月份是从0开始的,0-11
创建日期对象及基本使用方法
创建日期对象
var a = new Date();
日期对象方法:
格式:Date.parse(日期对象);
功能:将日期对象转为毫秒数(从1970年1月1日00:00:00到当前时间有多少毫秒)。
获取日期
- 获取当前时间
alert(a);
2. 获取当前年份
alert(a.getFullYear());
- 获取当前月份
alert(a.getMonth());
- 获取当前日(几号)
alert(a.getDate());
- 获取当前小时(几点)
alert(a.getHours());
- 获取当前分钟(几分)
alert(a.getMinutes());
7.获取当前秒(几秒)
alert(a.getSeconds());
- 获取当前毫秒(几毫秒)
alert(a.getMilliseconds());
- 获取当前时间至1970年1月1日00:00:00有多少毫秒
alert(a.getTime());
- 获取本地时间与格林威治时间的分钟差
alert(a.getTimezoneOffset());
- 获取当前毫秒数(从1970年1月1日00:00:00到当前时间有多少毫秒[一分钟=1000毫秒])
alert(a.getTime());
修改日期
- 修改当前日期的年份
a.setFullYear(2020);
alert(a);
2. 修改当前日期的月份
a.setMonth(4);
alert(a);
- 修改当前日期的日
a.setDate(24);
alert(a);
- 修改当前小时(几点)
a.setHours(9);
alert(a);
- 修改当前分钟(几分)
a.setMinutes(34);
alert(a);
- 修改当前秒(几秒)
a.setSeconds(25);
alert(a);
- 修改当前毫秒(几毫秒)
a.setMilliseconds(456);
alert(a);
- 修改当前时间至1970年1月1日00:00:00有多少毫秒
a.setTime(10);
alert(a);
- 修改本地时间与格林威治时间的分钟差
a.setTimezoneOffset();
alert(a);
- 修改当前毫秒数(从1970年1月1日00:00:00到当前时间有多少毫秒[一分钟=1000毫秒])
a.setTime(4838400000);
alert(a);
4838400000毫秒是56天
计算方式:562460601000
特殊方法
以特定格式显示 星期几 月 日 年
alert(d.toDateString());
以特定格式显示 时 分 秒 时区
alert(d.toTimeString());
以特定格式显示 年 月 日
alert(d.toLocaleDateString());
以特定格式显示 时刻 时 分 秒
alert(d.toLocaleTimeString());
以特定格式显示 月 日 年 时 分 秒 当前日期以格林威治日期显示(英国时间)
alert(d.toUTCString());
日期例子
日期格式化
function nowTime() {
var a = new Date();
var year = a.getFullYear();
var month = a.getMonth() + 1;
var date = a.getDate();
var day = a.getDay();
var hour = a.getHours();
var minutes = a.getMinutes();
var seconds = a.getSeconds();
var str = year + "年" + doubleTime(month) + "月" + doubleTime(date) + "日 " + "星期" + week(day) + " " + hour + ":" + doubleTime(minutes) + ":" + doubleTime(seconds);
return str;
}
document.write(nowTime());
function doubleTime(num) {
if (num < 10) {
return "0" + num;
} else {
return num;
}
}
function week(num) {
var arr = ["日", "一", "二", "三", "四", "五", "六"];
return arr[num];
}
输出结果为当前时间,显示格式:年 月 日 星期几 时 分 秒
获取两个日期之间有几天
function countTime(n) {
var a = new Date();
var b = new Date(n);
var time1 = Date.parse(a);
var time2 = Date.parse(b);
var time = Math.abs(time2 - time1);
return parseInt(time / 1000 / 60 / 60 / 24);
}
document.write(countTime("2021/2/5"));