【ES6】class类详解

       ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对 象的模板。通过 class 关键字,可以定义类。基本上,ES6 的 class 可以看作只是 一个语法糖,它的绝大部分功能,ES5 都可以做到,新的 class 写法只是让对象 原型的写法更加清晰、更像面向对象编程的语法而已。

类:是一个抽象的概念

对象:是类的实例化

目录

知识点:

class 声明类

constructor 定义构造函数初始化

static 定义静态方法和属性

extends 继承父类

super 调用父级构造方法

父类方法可以重写

知识点:

  1. class 声明类
  2. constructor 定义构造函数初始化
    class Shoujie {
    
            constructor(price,branch){
    
                this.price = price;
    
                this.branch = branch;
    
            }
    
            call(){
    
                console.log(`${this.price}:${this.branch}`);
    
            }
    
        }
    
    
    
        let huawei = new Phone(2859,"华为");
    
        huawei.call();
  3. static 定义静态方法和属性
     class Shouji {
    
            static name = "手机";
    
    		static change(){
    
    			console.log("可以改变 世界");
    
    		}
    
            constructor(price,branch){
    
                this.price = price;
    
                this.branch = branch;
    
            }
    
            call(){
    
                console.log(`${this.price}:${this.branch}`);
    
            }
    
        }
  4. extends 继承父类
  5. super 调用父级构造方法
  6. 父类方法可以重写
function Phone(price,branch){

        this.price = price;

        this.branch = branch;

    }

    Phone.prototype = {

        call:function(){

            console.log(`${this.price}:${this.branch}`);

        }

    }

    function SmartPhone(price,branch,color,size){

        Phone.call(this,price,branch);

        this.color = color;

        this.size = size;

    }

    SmartPhone.prototype = new Phone();

    SmartPhone.prototype.photo = function(){

        console.log("我可以拍照");

    }

    let sp = new SmartPhone(2999,"小米","black","5.5inch");

    sp.call();

    sp.photo();
  class Shouji {

        static name = "手机";

        constructor(price,branch){

            this.price = price;

            this.branch = branch;

        }

        call(){

            console.log(`${this.price}:${this.branch}`);

        }

    }



    class xiaoshouji extends Shouji {

        constructor(price,branch,color,size){

            super(price,branch);

            this.color = color;

            this.size = size;

        }

        photo(){

            console.log("我可以拍照");

        }

        playGame(){

            console.log("我可以打游戏");

        }
        call(){
            
            console.log("我可以视频通话");
            
        }

    }

    let huawei = new xiaoshouji(2999,"华为","red","5.5inch");

    console.log(huawei);    

    huawei.playGame();

    huawei.call();

    huawei.photo();

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

早睡第一人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值