一.数组类型
1.1 对象
若干个键值对
var 对象名{
属性名:属性值,
属性名:属性值,
属性名:属性值
}
var person={
name:"xiaoming",
age:19,
email:"23839586471@qq.com",
score:0
}
js中对象,{.....}表示一个对象,键值对描述属性xxxx: xxxx ,多个属性之间使用逗号隔开,最后一个属性不加逗号!
javascript中的所有的键都是字符串,值是任意对象!
1.对象赋值
person.name="ply"
'ply'
person.name
'ply'
2.使用一个不存在的对象属性,不会报错!
person.haha
undefined
3.动态的删减属性,通过delete删除对象的属性
person
{name: 'ply', age: 19, email: '23839586471@qq.com', score: 0}
delete person.name
true
person
{age: 19, email: '23839586471@qq.com', score: 0}
4.动态的添加,直接给新的属性添加值即可
person
{age: 19, email: '23839586471@qq.com', score: 0}
person.haha="qyn"
'qyn'
person
{age: 19, email: '23839586471@qq.com', score: 0, haha: 'qyn'}
5.判断属性值是否在这个对象中! xxx in xxx!
'age' in person
true
//继承
'toString' in person
true
6.判断一个属性是否是这个对象自身拥有的
person.hasOwnProperty('age')
true
person.hasOwnProperty('toString')
false
1.2 流程控制
1. if 判断
var age=3;
if(age>3){//第一个判断
alert("haha");
}else if(age<5){//第二个判断
alert("ply");
}else{//否则
alert(""ply11)
}
2. while循环,避免程序死循环
while(age<100){
age=age+1;
console.log(age)
}
do{
age=age+1;
console.log(age)
}while(age<100)
3.for循环
for(let i=0;i<100;i++){
console.log(i)
}
4.forEach循环
var age= [12,3,12,3,12,3,12,31,23,123];
//函数
age.forEach(function (value){
console.log(value)
})
5.for... in
//for(var index in object){}
for(var num in age){
if(age.hasOwnProperty(num)){
console.log("存在")
console.log(age[num])
}
}
1.3 Map和Set
1)Map
//ES6 Map
//学生的成绩,名字
//var names= ["tom","jack","selinna"];
//var scores=[100,90,92];
var map =new Map([['tom',100],['jack',98],['justin',89]]);
var name = map.get('tom');//通过key获得value
map.set('admin',123456);//相当于新添加一个元素在数组里面
//{'tom' => 100, 'jack' => 98, 'justin' => 89, 'admin' => 123456}
console.log(name);
map.delete("tom");//删除元素
2)Set:无序不重复集合
var set = new Set([3,1,1,1,1]);//会去重 所以打印出来只有两个元