ES6之class类

    //手机构造函数
        function Phone(brand, price) {
            this.brand = brand;
            this.price = price;
        }

        //添加 方法
        Phone.prototype.call = function() {
                console.log('我打电话')
            }
            //实例化对象

        let HuaWei = new Phone('华为', 5888);
        HuaWei.call();
        console.log(HuaWei);

        //class

        class Phone1 {
            //构造方法 名字不能改变
            constructor(brand, price) {
                    this.brand = brand;
                    this.price = price;
                }
                //方法必须使用该语法
            call() {
                console.log("我打电话");
            }
        }

        let oneP = new Phone1('xiaomi', 1222);
        console.log(oneP);

 

posted @ 2022-10-25 16:26  小白字太白  阅读(10)  评论(0编辑  收藏  举报