遍历对象的属性和方法\in运算符:判断对象中是否存在某个属性或方法\对象数组\数组中存放的是对象(数组中的每个元素都是对象)\JavaScript的内置对象(MDN,Math对象)

一.遍历对象的属性和方法:

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
  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值