ES6 - 類(lèi)

類(lèi)的基本使用

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // 構(gòu)造函數(shù)的方式
        function Person(name, age)
        {
            this.name = name;
            this.age  = age;
        }

        var person = new Person('Peter', 30);
        console.log(person);

        // 類(lèi)的方式
        class Student
        {
            // 構(gòu)造方法
            constructor(name, age)
            {
                this.name = name;
                this.age  = age;
            }
        }

        var student = new Student('Tom', 18);
        console.log(student);
    </script>
</body>
</html>

類(lèi)中的方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // 構(gòu)造函數(shù)的方式
        function Person(name, age)
        {
            this.name = name;
            this.age  = age;
        }

        Person.prototype.say = function() {
            console.log(`I am ${this.name}, I am ${this.age}`);
        }

        var person = new Person('Peter', 30);
        person.say();

        // class 形式
        class Student
        {
            // 構(gòu)造方法
            constructor(name, age)
            {
                this.name = name;
                this.age  = age;
            }
            say() {
                console.log(`I am ${this.name}, I am ${this.age}`);
            }
        }

        var student = new Student('Tom', 18);
        student.say();
    </script>
</body>
</html>

類(lèi)的靜態(tài)方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        class Student
        {
            // 構(gòu)造方法
            constructor(name, age)
            {
                this.name = name;
                this.age  = age;
            }
            static say() {
                console.log('Hellow');
            }
        }

        // 只能通過(guò)類(lèi)本身訪(fǎng)問(wèn)
        Student.say();
    </script>
</body>
</html>

類(lèi)的繼承

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        class Person
        {
            // 構(gòu)造方法
            constructor(name, age)
            {
                this.name = name;
                this.age  = age;
            }
            say() {
                console.log(`I am ${this.name}, I am ${this.age}`);
            }
        }

        class Student extends Person
        {

        }

        class Teacher extends Person
        {
            // 構(gòu)造方法
            constructor(name, age, gender)
            {
                // 必須調(diào)用父類(lèi)構(gòu)造方法,否則會(huì)報(bào)錯(cuò)
                super(name, age);

                this.gender = gender;
            }
            // 重寫(xiě)父類(lèi) Person 的 say 方法
            say() {
                console.log(`I am ${this.name}, I am ${this.age}, I am a teacher`);
            }
        }

        var student = new Student('Tom', 18);
        student.say();

        var teacher = new Teacher('Peter', 30, '男');
        teacher.say();
    </script>
</body>
</html>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前言 最近這兩天有點(diǎn)不在狀態(tài),感覺(jué)有種無(wú)形的壓力壓在身上,變的異常的暴躁了,唉,前端這條路真的走對(duì)了嗎?算了,敲代...
    aymfx閱讀 331評(píng)論 0 0
  • 一、簡(jiǎn)介 JavaScript 語(yǔ)言中,生成實(shí)例對(duì)象的傳統(tǒng)方法是通過(guò)構(gòu)造函數(shù)。下面是一個(gè)例子。 ES6 提供了更接...
    threetowns閱讀 516評(píng)論 1 0
  • 這次我們來(lái)講解一下關(guān)于ES6類(lèi)以及繼承操作,在此之前,我們先來(lái)說(shuō)一下關(guān)于ES5是如何實(shí)現(xiàn)類(lèi)以及類(lèi)的繼承的吧 廢話(huà)不...
    喲喲喲煎餅果子閱讀 737評(píng)論 0 2
  • 面向?qū)ο蟮恼Z(yǔ)言都有一個(gè)類(lèi)的概念,通過(guò)類(lèi)可以創(chuàng)建多個(gè)具有相同方法和屬性的對(duì)象,ES6之前并沒(méi)有類(lèi)的概念,在ES6中引...
    Erric_Zhang閱讀 1,209評(píng)論 1 4
  • ES6之類(lèi) 和大多數(shù)面向?qū)ο蟮恼Z(yǔ)言(object-oriented programming language)不同...
    鋒享前端閱讀 438評(píng)論 0 0

友情鏈接更多精彩內(nèi)容