010 Javascript(109-)

[A] ECMA6的class语法

            class: ECMA6中用来创建类的方法,可以替代传统的构造函数创建类的方法。

            1. 创建类:

        a. 类属性: 通过constructor添加

        b. 类方法: 直接添加

                    案例:

            class Person{
                // constructor添加类属性
                constructor(){
                    this.name = name;
                    this.gender = gender;
                }
                // 直接添加类方法
                showName(){
                    alert(`我名字叫${this.name}`);
                }
                showGender(){
                    alert(`我是一名${this.gender}性`);
                }
            }

            2. 继承

                  extends继承
                案例:
           class iPerson extends Person{
               constructor(name, gender, job){
                   //继承父一级的属性
                   super(name, gender);
                   //添加自己的属性
                   this.job = job;
                   // 原有方法已经自动继承
                }
                // 添加自己的方法
                showJob(){
                  alert(`我的职业是${this.job}.`)
                }
           }
原文地址:https://www.cnblogs.com/carreyBlog/p/13094826.html