es5和es6的类的继承方式

2019-07-29  本文已影响0人  LAYoung_f1b8

<script>

        // 定义普通飞机及属性

        // Plane.prototype.fly = function () {

        //    console.log('i can fly');

        // }

        // function Plane(name){

        //    this.name = name || '普通飞机';

        //    this.blood = 100;

        // }

        // var p = new Plane('小飞机');

        // // 定义战斗机及属性

        // AttackPlane.prototype.attack = function() {

        //    console.log('biu biu biu biu');

        // }

        // function AttackPlane(newName){

        //    Plane.call(this,newName);

        // }

        // var Ap = new AttackPlane('战斗机');

        // // AttackPlane继承Plane的所有属性方法

        // Object.setPrototypeOf(AttackPlane.prototype,Plane.prototype);

        // ES6构造类

        class Plane{

            // 私有属性 定义在constructor里面

            constructor(name){

                this.name = name || '普通飞机';

                this.blood = 100;

            }

            // 原型方法,共有属性  继承之后都会有的属性

            fly() {

                console.log('i can fly');

            }

            // 静态方法

            static alive() {

                console.log('i am alived');

            }

        }

        var op = new Plane('小飞机');

        // 两个类之间的继承

        class AttPlane extends Plane{

            constructor(newName){

                super(newName);

                this.logo = 'hope';

            }

            attack() {

                console.log('biu biu biu')

            }

            static nice() {

                console.log('nice one');

            }

        }

        var oap = new AttPlane('战斗机');

    </script>

上一篇 下一篇

猜你喜欢

热点阅读