es6中的面向对象写法

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Page Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
    <script>
        //创建Person类
        class Person {
            //构造函数
            constructor(name,age){
                this.name = name;
                this.age = age;
            }
            //方法
            say(){
                alert(this.name)
            }
        }

        //创建Worker类 继承自 Person类
        class Worker extends Person {
            //Worker类的构造函数
            constructor(name,age,work){
                //继承Person类中的属性
                super(name,age);
                this.work = work;
            }
            //方法
            sayWork(){
                alert(this.work)
            }
        }

        //生成实例
        let w = new Worker('李三',18,'搬砖');
        
        //调用方法
        w.say();
        w.sayWork();
    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/javascripter/p/10422972.html