day08
知识回顾:
1.函数封装
将功能的实现代码封装到函数内部、将变化的量提取为参数(有需要时,将结果返回)
2.Math对象
终点掌握常用方法
3.Date对象
get系列方法 取值 取时间/日期/星期
知识点补充:
1.获取当前时间 new Date()
2.获取指定时间:
//方法一;
var d=new Date("2019/3/12");
var d=new Date("2019-3-12");
//方法二:
// var d=new Date(2021,5,20);// 6月
var d=new Date(2021,7,7,12,0,0);// 2021/8/7 12:00:00
//方法三: 2030/9/9 10:00:00
var d = new Date();//当前时间
d.setFullYear(2030);
d.setMonth(8);//9月
d.setDate(9);
d.setHours(10);
d.setMinutes(0);
d.setSeconds(0);
3.时间本地化
var d=new Date();
console.log(d);
console.log(d.toTimeString()); //09:28:06 GMT+0800 (中国标准时间)
console.log(d.toDateString()); //Wed Mar 10 2021
console.log(d.toLocaleString());//本地化时间日期字符串
console.log(d.toLocaleDateString());//本地化日期字符串
console.log(d.toLocaleTimeString());//本地化时间字符串
4.moment.js 日期时间的库
Y 年 M 月 D 日 h 小时 m 分钟 s 秒
//引入moment.js
//使用
moment();//主函数
moment().format(格式化算子);
//示例:
// var d=moment().format()
// var d=moment().format("YYYY-MM-DD hh:mm:ss");
// var d=moment().format("YYYY/MM/DD hh:mm:ss");
// var d=moment().format("YYYY年MM月DD日 hh:mm:ss");
var d=moment().format("YYYY年MM月DD日 h:m:s");//2021年03月10日 9:41:19
一、String对象
字符串对象
索引 从0开始
长度:length属性
‘’ “” 表示字符串
遍历:for循环
var s=new String('abc');
var s1="hello";
1.常见的方法
-
charAt(index) 根据索引取出对应的字符
var str='hello'; var c=str.charAt(0);'h' //根据索引取出字符串中的字符
-
charCodeAt(index) 根据索引取出对应字符的ASCII码
// var s = '0'; // var cc = s.charCodeAt(0);//索引为0的字符的ASCII码 // console.log(cc);
-
indexOf() 获取指定字符/字符串 在母串中第一次出现的位置 找到 index 找不到 -1
-
常用语判别字符串中是否包含指定的字符
pic.onclick = function () { // console.log(this.src); //路径中是否包含1.jpg if (this.src.indexOf('1.jpg') != -1) { //不等于-1 当前就是1.jpg this.src = "./img/2.jpg"; } else { this.src = "./img/1.jpg"; } };
-
-
lastIndexOf() 获取指定字符/字符串 在母串中第一次出现的位置 从后向前查找
-
字符串截取
- substring(start,stop) 从start开始,截取到stop [start,stop)
- slice(start,end) [start,end)
- substr(start,length) 从start开始,截取length个字符
//substring // console.log(s.substring(0,3));// abc // console.log(s.substring(2));//'cdefg' // console.log(s.substring(3,100));// defg //slice() // console.log(s.slice(0,3));// abc // console.log(s.slice(0));// abcdefg // console.log(s.slice(3,100));// defg //substring和slice的区别 //substring中,左start的值大于stop的值,则它自动调整位置 slice不可以 // console.log(s.substring(5,3));// de // console.log(s.slice(5,3));// '' // 若第二个参数为负值,slice会从后向前截取 最后一个字符的位置-1,倒数第二个-2,,以此类推 // console.log(s.substring(3,-1));// abc console.log(s.slice(3,-1));// def console.log(s.slice(3,-2));// de
-
大小写转换
- toLowerCase() 转换为小写
- toUpperCase() 转换为大写
-
split() 使用指定字符将字符串分隔为数组
- 可以搭配数组对象的join()方法来实现数组和字符串之间的互相转换
-
repalce(oldChar,newChar) 替换字符串,,只能替换第一个
-
trim() 祛除字符串首尾空格
二、Array对象
Array就是数组对象
索引,数组中每一个元素都有一个下标,从0开始
length属性:数组中元素的个数
创建数组
// var arr=new Array();//创建一个数组示例
// arr[0]=100;
// // console.log(arr);
// arr[1]=200;
// // console.log(arr);
// arr[5]=300;
// console.log(arr);
// console.log(arr.length);
// var arr=new Array(10,20,100,90);
// console.log(arr);
// arr[0]='hello';//覆盖
// console.log(arr);
// var arr=new Array(3);//声明一个长度为3的数组
// console.log(arr,arr.length);
// arr[5]=100;
// console.log(arr);
// var arr=[];
var arr0=[10,20,100];
arr0[0]='你好';
console.log(arr0);
1.常用的方法
1.1 数组增加、删除方法
- push() 数组末尾追加元素
- push(item) 添加一个元素
- push(item,item,…) 添加多个元素
- pop() 删除数组末尾的元素
- shift() 删除数组头部元素
- unshift() 在数组头部添加元素
- unshift(item) 加一个
- unshift(item,item,…) 加多个
1.2 splice
splice(start,deleteCount,item1,item2,…,itemx)
参数说明:
- start 索引,从哪里开始操作 必选
- deleteCount 个数/删除的元素的个数
- item1,…,itemx 元素
var arr=[1,2,3,5,7];
//1. 删除数组元素
// arr.splice(0,2);
// arr.splice(1,1);
// arr.splice(1,0);
//2.添加元素
// arr.splice(1,0,10);//在索引为1的位置,添加一个元素
// arr.splice(2,0,100,200);
//3.替换
// arr.splice(1,1,100);
// arr.splice(1,3,100);
arr.splice(1,1,900,100,200,300);
console.log(arr);