对象
对象是指一个具体的事物。万物皆对象。
一个具体的事物一般都会有行为和特征。
对象的行为------js对象的函数
对象的特征------js对象的属性
JavaScript中的对象
JavaScript中的对象可以看做生活中具体事物的抽象。
js对象是属性+函数的集合。
属性:可以是基本数据类型,也可以是引用数据类型。
- 创建一个对象
- 使用new关键字类创建
var hero = new Object();
- 使用字面量
var hero = {}
- 新增属性
hero.arr = "托儿索";
hero.grade = '18';
hero['ana'] = '疾风亦有归途';
- 使用属性
对象名.属性名
// 使用属性
alert(hero.arr);
alert(hero.ana);
- 新增方法
对象名.属性名=function(){
函数体
};
hero.talk = function() {
alert('永恩:亚索接好了');
alert('亚索:面对疾风吧');
alert('哈撒给');
}
// 调用函数
hero.talk();
- 删除属性
delete 对象名,属性名
var hero = {
"heroName": "亚索",
"age": 30,
skillOne: function() {
console.log("斩钢闪");
},
skillThree: function() {
console.log("E去不复返");
}
};
delete hero.age;
alert(hero.age);
alert(hero.heroName);
这里hero.age被删除。
输出为未定义。
Math对象
提供了很多关于运算的方法。
Math.random(); 返回0-1之间的随机数。
假如我们需要一个n-m之间的数。
Math.round(Math.random()*(m-n)+n)
Math.floor(Math.random() * (m - n + 1) + n);
Math.round()四舍五入
Math.max() 返回最大值。
Math.min() 返回最小值
Math.abs() 返回绝对值
Math.ceil()向上取整
Math.floor() 向下取值
Math.pow(x,y) x的y次方
Math.sqrt(num) 开平方
0到10的随机数
示例:
// 0到10的随机数
alert(Math.floor(Math.random() * 10));
- Math.round()四舍五入
示例:
// 四舍五入
alert(Math.round(4.6));
- Math.max() 返回最大值。
示例:
// 返回最大值
alert(Math.max(1, 2, 3, 9, 4));
输出结果9
- Math.min() 返回最小值
示例:
// 返回最小值
alert(Math.min(7,5,9,1));
- Math.abs() 返回绝对值、
示例:
// 返回绝对值
alert(Math.abs(-5));
- Math.ceil()向上取整
示例:
// 向上取整
alert(Math.ceil(4.5));
- Math.floor() 向下取值
示例:
// 向下取整
alert(Math.floor(4.9));
- Math.pow(x,y) x的y次方
示例:
// x的y次方
alert(Math.pow(2, 3));
结果为2的三次方=8.
- Math.sqrt(num) 开平方
示例:
// 开次方(开根)
alert(Math.sqrt(25));
这里做一个小练习。
获取随机16进制的颜色
如#ffffff,#12aa12
function getColor() {
var str = "#";
var arr = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "b", "c", "d", "e", "f"];
for (var i = 0; i < 6; i++) {
str += arr[Math.floor(Math.random() * 16)];
}
return str;
}
document.write(getColor());
随机输出16进制颜色。
Date对象
创建时间对象。
Wed Jan 06 2021 14:38:02 GMT+0800 (中国标准时间)
星期 月 日 年 时分秒 时区
创建日期对象是,可以指定日期。时分秒若不填写,则默认为00:00:00
- 指定日期的几种方式:
- new Date(“2021/01/07”)
- new Date(2020,09,30,15,33,20);
- new Date(1576800000000);毫秒,从1970年开始读取。
- new Date(“2020-12-31 12:30:20”);
月份是从0月开始计数的,0——11
显示时间示例:
//显示当前时间
var d = new Date();
alert(d);
var d = new Date("2021/01/07");
alert(d);
时分秒若不填写,则默认为00:00:00
var d = new Date(2020,09,30,15,33,20);
alert(d);
指定时间。
var d = new Date(1576800000000);
alert(d);
毫秒,从1970年开始读取。
var d = new Date("2020-12-31 12:30:20");
alert(d);
日期对象方法
格式:Date.parse(日期对象);
功能:将日期对象转为毫秒数。
var d = new Date();
var result = Date.parse(d);
alert(result);
当前时间转换为毫秒
d.getTime() 获取当前的毫秒数。
d.getDate() 返回日
d.getDay() 返回周几。
d.getMonth() 获取月份 从0开始 范围为:0-11
d.getFullYear() 返回年
d.getHours() 获取小时数
d.getMinutes() 获取分钟数
d.getSeconds() 获取秒数
d.getMilliseconds() 获取毫秒数
d.getTimezoneOffset() 获取本地时间与格林威治时间的分钟差。
// 当前日期转毫秒
var d = new Date();
var a = d.getTime();
alert(a);
// 获取日
var d = new Date();
var a = d.getDate();
alert(a);
// 获取周几
var d = new Date();
var a = d.getDay();
alert(a);
// 获取月 月是从0开始到11结束 0——11
var d = new Date();
var a = d.getMonth();
alert(a);
// 获取年
var d = new Date();
var a = d.getFullYear();
alert(a);
// 获取当前小时数
var d = new Date();
var a = d.getHours();
alert(a);
// 获取分钟数
var d = new Date();
var a =d.getMinutes();
alert(a);
// 获取秒数
var d = new Date();
var a = d.getSeconds();
alert(a);
自定义时间格式
代码如下
<script>
var d = new Date();
// 年
var year = d.getFullYear();
// 月
var to = d.getMonth() + 1;
// 日
var or = d.getDate();
// 时
var hours = d.getHours();
// 分
var min = d.getMinutes();
// 秒
var secon = d.getSeconds();
// 星期
var week = d.getDay();
function toCustom() {
var d = year + '年' + scale(to) + '月' + scale(or) + '日' + ' ' + '星期' + characters(week) + ' ' + scale(hours) + ":" + scale(min) + ':' + scale(secon);
return d;
}
// 定义<10的数前面加0;
function scale(n) {
if (n < 10) {
return "0" + n;
} else {
return n;
}
}
// 数字转文字
function characters(n) {
var day = ['日', '一', '二', '三', '四', '五', '六'];
return day[n];
}
var d = toCustom()
alert(d);
</script>