ES6

ES6 - 类与继承

2019-01-15  本文已影响0人  饮杯梦回酒

导读:

例子:

<!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>
<!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>

总结:

上一篇下一篇

猜你喜欢

热点阅读