美文网首页ES6
ES6 - 类与继承

ES6 - 类与继承

作者: 饮杯梦回酒 | 来源:发表于2019-01-15 17:06 被阅读0次

导读:

  • 以前ES5的时候也有构造函数和继承这一说法,但是实现起来又要借助原型又要改this指向,就会显得很麻烦,而ES6出来了这个主流的类这么个概念,几乎就跟Java里面的类与继承实现一模一样,看代码。

例子:

  • 构造函数比较:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // ES5的构造函数
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }
        Person.prototype.showName = function() {
            return `名字是: ${this.name}`;
        }
        Person.prototype.showAge = function() {
            return `年龄是: ${this.age}`;
        }

        // ES6语法模拟
        /*Object.assign(Person.prototype, {
            showName() {
                return `名字是: ${this.name}`
            },
            showAge() {
                return `年龄是: ${this.age}`
            }
        })*/

        let p1 = new Person('Verin', 18);
        console.log(p1.showName());
      
        //  ES6的类(构造函数)
        class Person {
            constructor(name, age) {
                // 构造函数,用法同java的构造函数就行了
                this.name = name;
                this.age = age;
            }
            showName() {
                return `名字是: ${this.name}`
            }
            showAge() {
                return `年龄是: ${this.age}`
            }
        }

        let p1 = new Person('Verin', 18);
        console.log(p1.showName(), p1.showAge())

    </script>
</body>
</html>
  • 继承比较:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <script type="text/javascript">
        // ES5继承
        function Person(name) {
            this.name = name
        }
        Person.prototype.showName = function() {
            return `我的名字是: ${this.name}`;
        }

        function Student(name, skill) {
            Person.call(this, name) // 继承属性
            this.skill = skill;
        }
        Student.prototype = new Person();

        let s1 = new Student('Verin', '逃学');
        console.log(s1.name, s1.skill);   //  Verin, 逃学


        // ES6继承
        class Person2 {
            constructor(name) {
                this.name = name
            }
            showName() {
                return `我的名字是: ${this.name}`;
            }
        }

        class Student2 extends Person2 {
            constructor(name, skill) {
                super(name);
                this.skill = skill;
            }
            showSkill() {
                return `哥们的技能是: ${this.skill}`
            }
        }

        let s2 = new Student2('Verin', '逃学');
        console.log(s2.name, s2.showSkill());   //  Verin, 哥们的技能是: 逃学

    </script>
</body>
</html>
  • 子类可以在继承父类的所有属性方法外,新增自己的属性方法,若要在已有方法上新增功能,则需先执行super() || super.方法()后再写入自己定义的功能,如下简单实现拖拽,子类新增限制拖拽范围功能:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <style type="text/css">
        .box {
            width: 100px;
            height: 100px;
            background-color: red;
            position: absolute;
        }
        .left {
            left: 0;
        }
        .right {
            right: 0;
        }
    </style>
</head>
<body>
    <div class="box left">box1</div>
    <div class="box right">box2</div>
    <script type="text/javascript">
        class Drag {
            constructor(id) {
                this.oDiv = document.querySelector(id);
                this.disX =0;
                this.disY = 0;
                this.init();
            }
            init() {
                this.oDiv.onmousedown = function(ev) {
                    this.disX = ev.clientX - this.oDiv.offsetLeft;
                    this.disY = ev.clientY - this.oDiv.offsetTop;
                    document.onmousemove = this.fnMove.bind(this);
                    document.onmouseup = this.fnUp.bind(this);

                    return false;
                }.bind(this)
            }
            fnMove(ev) {
                this.oDiv.style.left = ev.clientX - this.disX + 'px';
                this.oDiv.style.top = ev.clientY - this.disY + 'px';
            }
            fnUp() {
                document.onmousemove = null;
                document.onmouseup = null;
            }
        }

        // 子类 —— 限制距离
        class LimitDrag extends Drag {
            fnMove(ev) {
                super.fnMove(ev);

                // 限制范围
                if(this.oDiv.offsetLeft <= 0) this.oDiv.style.left = 0;
            }
        }

        new Drag('.left');
        new LimitDrag('.right');
    </script>
</body>
</html>

总结:

  • 如果有Java或者其他主流编程语言的基础,一般都知道类与继承,这里理解就会很容易,否则需多花时间弄明白,这个是很重要的!

相关文章

  • 面向对象类

    类与实例 类的声明 ES5 ES6 生成实例 类与继承 如何实现继承 继承的几种方式 原型链是实现继承的主要方法 ...

  • ES的类与继承

    ES5中的类与继承 构造函数继承,原型继承,组合式继承 静态方法,静态属性,实例方法,实例属性 ES6中的类与继承...

  • ES6

    ES6是一个语言标准,不是一个框架。 ES6中的class与继承 class是创建类对象与实现类继承的语法糖,旨在...

  • 继承

    老版继承 call () ES6继承 extends 继承super 超类/父类

  • react组件

    es6 的class类的继承 运用es6 class继承 通过继承React.Component类来定义一个组件

  • javascript之模拟类继承

    前言 ES6时代的来临,使得类继承变得如此的圆滑。但是,你有思考过ES6的类继承模式吗?如何去实现它呢? 类继承对...

  • JS中类的继承封装和多态

    子类继承父类的属性和方法(原生继承,call继承,寄生组合继承,ES6中class类继承)原生继承:让子类的原型指...

  • es5 es6静态方法、类、单例模式

    es5中的类和静态方法 es5继承 es6中的类 es6里面的继承 es6里面的静态方法 es6单例模式 转载:h...

  • ES6中类与继承的理解(java对比记忆)

    ES6中类与继承的理解(java对比记忆) 先上两段代码:java中定义类: Es6中定义一个类: 通过上面两端代...

  • JavaScript 面向对象编程

    写法 ES6面向对象的写法---类继承

网友评论

    本文标题:ES6 - 类与继承

    本文链接:https://www.haomeiwen.com/subject/ppvydqtx.html