ES6 Class Demo

關鍵知識點:

1、類

2、命名定義類

3、構造函數

4、靜態類

5、類的繼承

6、箭頭函數

7、模板表達式

 

<html>
<head>
<title>類的測試</title>

<script type="text/javascript">
      const Person=class{
        constructor(name){
          this.name=name; 
          console.log(this.name);
        }

        showName(){
            console.log(`這是一個名字:${this.name}`);
            return this.name;
        }

        static showNameStatic(){
            console.log(`這是一個靜態類:${this.name}`);
        }

        canReshowName(){
           return `這是一個可以被重寫的方法:${this.name}`;
        }
    }

    class Student extends Person{

         a=()=>{
           console.log('aa');
         }
         constructor(name,skill){
            super(name);
            this.skill=skill
         }
         
         showSkill(){
            console.log(`這是一個技能:${this.name}`);
            return this.skill;
         }

         canReshowName(){
            super.canReshowName();
            console.log('這是子類的方法');
         }
    }

    let student=new Student('Lily','上課');
    console.log(student.showName(),student.showSkill());    
    Student.showNameStatic();
    student.canReshowName();
    student.a();
    </script>
</head>
<body>
    
</body>
</html>

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章