一.遍历对象的属性和方法:
1.使用for...in循环
2.对象访问属性或方法的方式:对象名['属性名或方法名']
二.in运算符:判断对象中是否存在某个属性或方法
<script>
let people = {
id:'1001',
name:'懒羊羊',
age:'18',
show:function(){
console.log('姓名:',this.name)
}
}
//1.for...in
for (let i in people) {
console.log(i+':'+people[i])//i既是people对象的属性名或方法名,也是索引
}
//2.对象名['属性名或方法名']
console.log(people.name)//懒羊羊
console.log(people['name'])//懒羊羊
console.log(people['show']())//出现undefined是因为没有返回值,上面的函数没写return语句
//3.判断people对象中是否有id这个成员
console.log('id'in people)//true
console.log('city'in people)//false
</script>
三.对象数组:数组中存放的是对象(数组中的每个元素都是对象)
<script>
// 1.创建一个构造方法:Student
function Student(name,city) {
this.name = name
this.city = city
this.html = 0//html属性的默认值为0
this.css = 0//css属性的默认值为0
this.show = function () {
console.log('姓名:',this.name)
console.log('城市:',this.city)
console.log('HTML成绩:',this.html)
console.log('css成绩:',this.css)
}
this.getSum = function () {
return this.css + this.html
}
}
// 2.创建对象
let s1 = new Student('懒羊羊','青青草原羊村1号')
s1.html = 88
s1.css = 66
let s2 = new Student('喜羊羊','青青草原羊村2号')
s2.html = 77