ES6系列笔记-面向对象/继承

简介: ES6系列笔记-面向对象/继承

一:面向对象

一般面向对象的写法

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
        //一般面向对象的写法
        function User(name, pass) {
            this.name = name;
            this.pass = pass;
        }
        User.prototype.showName = function() {
            console.log(this.name);
        }
        User.prototype.showPass = function() {
            console.log(this.pass);
        }
        var a = new User("wangting", "123456");
        a.showName();
        a.showPass();
    </script>
</html>

存在问题 : User既是构造函数,又是类,傻傻分不清

打印结果



ES6面向对象写法

好处:

1:class关键字,构造器 constructor和类User分开写

2:class里面直接写方法,方法不需要再外挂出去了

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
        //ES6面向对象写法
        class User {
            constructor(name, pass) {
                this.name = name;
                this.pass = pass;
            }
            showName() {
                console.log(this.name);
            }
            showPass() {
                console.log(this.pass);
            }
        }
        var a = new User("wangting", "123456");
        a.showName();
        a.showPass();
    </script>
</html>

打印结果


二:继承

继承一个已有类,扩展它的功能

一般的继承的写法(es5继承方法),这是在日常开发中经常能看到的写法。

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
        //一般写法
        function User(name, pass) {
            this.name = name;
            this.pass = pass;
        }
        User.prototype.showName = function() {
            console.log(this.name);
        }
        User.prototype.showPass = function() {
            console.log(this.pass);
        }
        //继承方法
        function Children(name, pass, level) {
            User.call(this, name, pass);
            this.level = level;
        }
        //方法
        Children.prototype = new User();
        Children.prototype.constructor = Children;
        Children.prototype.showLevel = function() {
            console.log(this.level)
        }
        var a = new Children("wangxiaoting", "123", 3);
        a.showName();
        a.showPass();
        a.showLevel();
    </script>
</html>

打印结果:


ES6的继承:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
        //ES6面向对象写法
        class User {
            constructor(name, pass) {
                this.name = name;
                this.pass = pass;
            }
            showName() {
                console.log(this.name);
            }
            showPass() {
                console.log(this.pass);
            }
        }
        //es6的继承(重点)
        class Children extends User {
            constructor(name, pass, level) {
                //继承父级属性
                //同时执行父类的构造函数
                super(name, pass);
                //继续写自己这一层该有的属性
                this.level = level;
            }
            //关于方法,就不需要像旧版new出来xx给.prototype了,因为 extends已经扩展完成了
            //直接写就行
            showLevel() {
                console.log(this.level);
            }
        }
        let c = new Children('wangxiaoting', '123', 23);
        c.showName();
        c.showPass();
        c.showLevel();
    </script>
</html>

打印结果:


相关文章
|
3天前
|
JavaScript 前端开发 程序员
TypeScript 类 第三章 【抽象类,类的高级技巧】
TypeScript 类 第三章 【抽象类,类的高级技巧】
54 2
|
3天前
|
Python
请简述Python中的继承、封装和多态的概念。
【2月更文挑战第24天】【2月更文挑战第82篇】请简述Python中的继承、封装和多态的概念。
|
3天前
|
Python
Python 面向对象编程:什么是面向对象编程(OOP)?解释封装、继承和多态的概念。
Python 面向对象编程:什么是面向对象编程(OOP)?解释封装、继承和多态的概念。
44 0
|
3天前
|
Python
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
Python从入门到精通:深入学习面向对象编程——2.1.2继承、封装和多态的概念
|
3天前
|
Python
python面向对象编程,解释继承和多态的概念。
python面向对象编程,解释继承和多态的概念。
|
9月前
|
Python
【从零学习python 】47. 面向对象编程中的继承概念及基本使用
【从零学习python 】47. 面向对象编程中的继承概念及基本使用
41 0
|
11月前
ES6 —— 类和对象
ES6 —— 类和对象
ES6 从入门到精通 # 22:类的继承
ES6 从入门到精通 # 22:类的继承
44 0
ES6 从入门到精通 # 22:类的继承
|
Java C++
Java开发——9.面向对象的特点(2.1):继承VS抽象
面向对象的特点:封装、继承和多态。
|
前端开发 Java
5、面向过程与面向对象、ES6与ES5的面向对象语法
5、面向过程与面向对象、ES6与ES5的面向对象语法
92 0
5、面向过程与面向对象、ES6与ES5的面向对象语法