ES6 - 类

类的基本使用

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

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

        // 类的方式
        class Student
        {
            // 构造方法
            constructor(name, age)
            {
                this.name = name;
                this.age  = age;
            }
        }

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

类中的方法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // 构造函数的方式
        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
        {
            // 构造方法
            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>

类的静态方法

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

        // 只能通过类本身访问
        Student.say();
    </script>
</body>
</html>

类的继承

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        class Person
        {
            // 构造方法
            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
        {
            // 构造方法
            constructor(name, age, gender)
            {
                // 必须调用父类构造方法,否则会报错
                super(name, age);

                this.gender = gender;
            }
            // 重写父类 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>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 前言 最近这两天有点不在状态,感觉有种无形的压力压在身上,变的异常的暴躁了,唉,前端这条路真的走对了吗?算了,敲代...
    aymfx阅读 370评论 0赞 0
  • 一、简介 JavaScript 语言中,生成实例对象的传统方法是通过构造函数。下面是一个例子。 ES6 提供了更接...
    threetowns阅读 546评论 1赞 0
  • 这次我们来讲解一下关于ES6类以及继承操作,在此之前,我们先来说一下关于ES5是如何实现类以及类的继承的吧 废话不...
    哟哟哟煎饼果子阅读 769评论 0赞 2
  • 面向对象的语言都有一个类的概念,通过类可以创建多个具有相同方法和属性的对象,ES6之前并没有类的概念,在ES6中引...
    Erric_Zhang阅读 1,249评论 1赞 4
  • ES6之类 和大多数面向对象的语言(object-oriented programming language)不同...
    锋享前端阅读 477评论 0赞 0

友情链接更多精彩内容