js高级---类与对象

对象的创建方法:

  1. 字面量
  2. new Object
  3. 构造函数
  4. 类的实例化
<script>
   // 1字面量方式
    var xx = {
        name: '蜡笔小新',
        age: 3
    }
    console.log(xx);


    // 2 new Object()方式
    var xx = new Object();
    xx.name = '蜡笔小新';
    xx.age = 3;
    xx.showSkill = function() {
        console.log('扮鬼脸');
    };
    console.log(xx);



    // 3构造函数方式
    function People(name, age) {
        this.name = name;
        this.age = age;
    }
    var xx = new People('蜡笔小新', 3) //实例化对象
    console.log(xx);
</script>

输出:
在这里插入图片描述

<script>
    //4 类的实例化
    class People {
        //类的共有属性放到 constructor 里面
        constructor(name, age) {
                this.name = name;
                this.age = age;
            }
            //注意:方法与方法之间不需要添加逗号
        sing(song) {
            console.log(this.name + '唱' + song);
        }
    }

    var xx = new People('蜡笔小新', 3);
    console.log(xx);
    xx.sing('动感超人');
</script>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值