JS内置对象 Math Date Array学习
Math
//Math 对象 是一个对象 不是构造函数 直接使用
//绝对值
console.log(Math.abs('-1')); //隐式转换
console.log(Math.abs('pink')); //NaN
//三个取整方法
//1.Math.floor() 向下取整
console.log(Math.floor(1.1));
//2.Math.ceil() 向上取整
console.log(Math.ceil(3.9));
//Math.round() 四舍五入 其他数字四舍五入 .5取大的那个值
console.log(Math.round(2.4));
console.log(Math.round(-1.5)); //-1
//Math.random()
//1、返回一个随机的小数 【0,1)
//2.没有参数
//3.代码验证
console.log(Math.random());
//4.两个数之间的随机整数且包含这两个整数 封装到函数里
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
//猜数字游戏
/* var random = getRandom(1, 50);
var i = 0;
while (i < 2) {
var num = prompt('你来猜');
if (num > random) {
alert('你猜大了');
} else if (num < random) {
alert('你猜小了');
} else {
alert('good');
break;
}
i++;
} */
Date
//日期对象Date
//Date是一个构造函数,必须new
var date = new Date();
console.log(date); //"2021-04-22T13:01:00.491Z" 没有参数返回当前时间
//console.log(date(2021, 4, 23)); //date not a function date是一个对象
//数字型参数和字符串参数
var date1 = new Date(2021, 4, 22);
console.log(date1); //返回的是五月 0~11
var date2 = new Date('2021-4-22 21:08:08');
console.log(date2); //返回的是四月
//日期格式化
console.log(date.getFullYear());
console.log(date.getMonth() + 1); //记得+1
console.log(date.getDate()); //返回几号
console.log(date.getDay()); //周日返回的是0
console.log(date.getHours()); //小时
console.log(date.getMinutes()); //分钟
console.log(date.getSeconds()); //秒
//2019年4月22日
var year = date.getFullYear();
var month = date.getMonth() + 1;
var date3 = date.getDate(); //date不能和对象名冲突
var arr = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
var day = date.getDay();
console.log(year + '年' + month + '月' + date3 + '日' + arr[day]);
//封装一个函数 获得当前时分秒
function getTimer() {
var time = new Date();
var h = time.getHours();
h = h < 10 ? '0' + h : h;
var m = time.getMinutes();
m = m < 10 ? '0' + m : m;
var s = time.getSeconds();
s = s < 10 ? '0' + s : s;
return h + '时' + m + '分' + s + '秒';
}
console.log(getTimer());
//获得Date总的毫秒数(时间戳) 距离1970年1月1日
//1.valueOF() getTime()
console.log(date.valueOf());
console.log(date.getTime());
//2.简单写法 最常用
var date4 = +new Date();
console.log(date4);
//3.h5新增(低版本不兼容)
console.log(Date.now());
//倒计时案例
//思路:输入时间减去当前时间获取相差的毫秒数 转换成天时分秒
function countDown(time) {
var nowTime = +new Date();
var inputTime = +new Date(time);
times = (inputTime - nowTime) / 1000; //将来的时间减去现在的时间,顺序不能变
var d = parseInt(times / 60 / 60 / 24);
d = d < 10 ? '0' + d : d;
var h = parseInt(times / 60 / 60 % 24);
h = h < 10 ? '0' + h : h;
var m = parseInt(times / 60 % 60);
m = m < 10 ? '0' + m : m;
var s = parseInt(times % 60);
s = s < 10 ? '0' + s : s;
return d + '天' + h + '时' + m + '分' + s + '秒';
}
console.log(countDown('2021-4-22 23:00:00'));
Array
//创建数组的两种方式
//1.数组字面量【】
var arr0 = [1, 2];
//2.new Array()
var arr1 = new Array(); //空数组
var arr2 = new Array(2); //2表示数组中有两个空数组元素
var arr3 = new Array(2, 3); // 创建了数组[2,3]
//检测是否为数组的两种方法
//1.instanceof 运算符
var arr = [];
var obj = {};
console.log(arr instanceof Array); //true
console.log(obj instanceof Array); //false
//2.Array.isArray() h5新增 ie9以上支持
console.log(Array.isArray(arr)); //true
console.log(Array.isArray(obj)); //false
//给数组添加,删除元素的方法
//1.push
console.log(arr0.push(3, 4)); //4
console.log(arr0); //[1,2,3,4]
// (1) push 可以给数组尾部添加元素
// (2) push() 参数是添加的元素,逗号隔开
// (3) 函数返回值是数组的个数
// (4) 原数组会发生变化
//2.unshift
arr0.unshift(0);
console.log(arr0); //[0,1,2,3,4]
// (1) unshift 可以给数组头部添加元素
// (2) unshift() 参数是添加的元素,逗号隔开
// (3) 函数返回值是数组的个数
// (4) 原数组会发生变化
//3.pop
arr0.pop();
// (1) pop 可以删除最后一个元素
// (2) pop() 没有参数
// (3) 函数返回值是被删除的元素
// (4) 原数组会发生变化
//4.shift
arr0.shift();
// (1) shift 可以删除第一个元素
// (2) shift() 没有参数
// (3) 函数返回值是被删除的元素
// (4) 原数组会发生变化
//数组排序
//1.翻转数组
arr0.reverse();
console.log(arr0);
//2.数组排序(冒泡排序)
arr0.sort(function (a, b) {
//return a - b; //升序
return b - a; //降序
});
//返回数组索引号的方法 indexOf
var arr5 = ['red', 'green', 'blue', 'blue'];
arr5.indexOf('blue'); //2
arr5.lastIndexOf('blue') //3
// (1)只返回第一个满足条件的索引
// (2)找不到返回 -1
//数组去重
//遍历旧数组,如果元素在新数组中不存在,则添加到新数组中
function unique(arr) {
var newArr = [];
for (var i = 0; i < arr.length; i++) {
if (newArr.indexOf(arr[i]) === -1) {
newArr.push(arr[i]);
}
}
return newArr;
}
console.log(unique([1, 2, 3, 4, 4, 4, 4])); //[1,2,3,4]
//把数组转换为字符串的两种方法
//1.toString()
var arr6 = [1, 2, 3]
console.log(arr6.toString()); //1,2,3 默认逗号分隔
//2.join('分隔符')
console.log(arr6.join('-')); //1-2-3
console.log(arr6.join('&')); //1&2&3
//数组的合并concat
arr7 = [1, 2, 3];
arr8 = [4, 5, 6];
var arr9 = arr7.concat(arr8);
console.log(arr9); //[1,2,3,4,5,6]
// (1)不会改变原数组,生成新的数组
// (2)返回一个新数组
//删除数组元素splice 返回被删除的元素组成的数组
//截取数组slice(start,end) 包括begin 不包括end 返回新数组