一:介绍对象
1.1对象
//1.对象[object]是什么:对象是一种复杂数据类型
//[复杂数据类型的内容存储在堆内存中,地址存储在栈内存中]
//2.对象作用:以键值对的形式存储多个数值
//需求:存储一个人的信息(姓名,年龄,性别)
//1.使用简单数据类型
let name="张三";
let age=38;
let sex="男"
//好处:阅读性高 弊端:冗余
//2.使用数组
var people=["张三",39,"男"]
//好处:一个变量存储多个数据 弊端:阅读性不高
//3.对象
let obj={
name:"张三",
age:30,
sex:'男'
}
1.2:查询对象
let obj={
name:"张三",
age:30,
sex:'男'
}
console.log(obj);//name:张三 age:30 sex:男
console.log(obj.name);//张三
console.log(obj.age);//30
console.log(obj.sex)//男
1.3:对象的修改与新增
//对象的新增
obj.bf="李四"
console.log(obj.bf)//李四
//对象修改
obj.bf="王五"
console.log(obj.bf)//"王五"
//多于对象的属性,如果有这个属性,是修改属性值,如果没有这个属性,则是新增
1.4:对象的删除
delete obj.age;
console.log(obj)
//bf: "王五"
//gfs: (3) ['lily', 'llll', 'asda']
//name: "张三"
//saytt: ƒ ()
//sex: "男"
1.5:对象的遍历
//遍历数组:for (let i=0;i<arr.length;i++)
//遍历对象:for-in循环:for(let key in 对象名){对象名[key]}
let score={
math:99,
history:80,
geo:66,
english:100,
}
for(let key in score){
console.log(key);
console.log(score[key]);
}
二:Math对象
2.1:内置对象
内置对象:是由JS作者提前编写好的对象
Math数学对象,Date日期对象
a.圆周率:Math.PI
//计算一个边长为5的圆的周长
const pi=3.14;
const r=5;
const C =2*pi*r;
console.log(C);//31.400000000000002
//a.圆周率 Math.PI
console.log(Math.PI);
const c=2*Math.PI*r
console.log(c);//31.41592653589793
b.绝对值:Math.abs()
console.log(Math.abs(-3.5));//3.5
c.求最大/最小值:Math.max()/Math.min()
console.log(Math.max(1,2,3,4,5,6,7,8,9,11));//11
console.log(Math.min(1,2,3,4,5,6,7,8,9,11));//1
d.幂运算:Math.pow(x,y) 求x的y次方
console.log(2,10);//1024
e.四舍五入:Math.round()
console.log(Math.round(3.5));//4
console.log(Math.round(3.2));//3
console.log(Math.round(-3.5));//-3
f.向上取整:Math.ceil()
console.log(Math.ceil(3.1))//4
console.log(Math.ceil(-3.1))//-3
g.向下取整:Math.floor()
console.log(Math.floor(-1.8))//-2
h.生成0-1之间的随机小数
console.log(Math.random());
三.Date对象
3.1:获取时间戳
时间戳:从1970年1月1日零时零分零秒到现在的毫秒数
//以下三种写法,都会得到时间戳
console.log(Date.now());
console.log(+new Date);
console.log(new Date().getTime());
3.2:获取年月日
//获取年份
console.log(new Date().getFullYear());
//获取月份
console.log(new Date().getMonth());//范围是0-11
//获取日期
console.log(new Date().getDate());//范围是1-31
//获取星期
console.log(new Date().getDay());//范围是0-6 星期天是0
//获取小时
console.log(new Date().getHours());
//获取分钟
console.log(new Date().getMinutes());
3.3:修改年月日
//获取内置Date对象
var date=new Date();
//设置年份
date.setFullYear(2004);
date.setMonth(2);
date.setDate(17);
//获取年份
var year1=date.getFullYear();
var month1=date.getMonth();
var date1=date.getDate();
console.log(year1);
console.log(month1);
console.log(date1);
四:Arr对象
4.1:splice
//splice (start,deleteCount)
var arr1=[1,2,3,4,5,6,7]
arr1.splice(3,2) //从下标为3的数(4)开始删掉,后面2个数字(包括4)
console.log(arr1); //[1,2,3,6,7]
//splice(start,deletCount,item1,item2,item3)
arr1.splice(4,0,999,888,777)//从下标为4的数字(6)开始,往后面添加一些元素
console.log(arr1);//[1, 2, 3, 6, 999, 888, 777, 7]
4.2:reverse(降序排序)
var arr1 =[1,2,3,4,5,6,7]
arr1.reverse()
console.log(arr1); //[7, 6, 5, 4, 3, 2, 1]
4.3:sort(升序排序)
//升序排序
var arr2=[19,45,29,0,18,73]
arr2.sort(function(a,b){
return a-b
})
console.log(arr2); //[0, 18, 19, 29, 45, 73]
4.4:获取数组元素索引
//indexOf(element) 返回指定元素在数组中第一次出现的索引,不存在则返回-1
//lastIndexOf(element) 返回指定元素在数组中最后一次出现的索引,不存在则返回-1
var color=["green","pink","red","pink","yellow","red"]
console.log(color.indexOf("red")); //2
console.log(color.indexOf("white")); //-2
console.log(color.lastIndexOf("red")); //5
4.5:将数组转化为字符串
//toString() 将数组转化为字符串,用逗号分隔数组中的每个元素
//join() 将数组中的所有元素连接成一个字符串。默认用逗号分隔,可指定分隔符
console.log(color); //['green', 'pink', 'red', 'pink', 'yellow', 'red']
console.log(color.toString()); //green,pink,red,pink,yellow,red
4.6:数据检测
var obj ={
name:"张三"
}
console.log(Array.isArray(arr)); //true
console.log(Array.isArray(obj)); //false
五:String对象
5.1:创建字符串对象
//创建字符串对象
var str=new String("apple")
console.log(str);//("apple")
5.2:根据字符串返回索引
//根据字符串返回索引
//indexOf() 返回字符串中首次出现的索引,没有则返回-1
//lastIndexOf(element) 返回字符串中最后一次出现的索引,没有则返回-1
var str="helloword"
console.log(str.indexOf("o")); //4
console.log(str.lastIndexOf("o")); //6
5.3:字符串截取
//字符串截取
//slice (start)
console.log(str.slice(5)); //"word"
//slice(start,end) 从start的下标截取到end的下标
console.log(str.slice(1,4));//ell""
console.log(str); //helloword 截取不会改变原数组