几种继承方式

3 篇文章 0 订阅

几种继承方式

1.构造函数继承和原型继承

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 面向对象: 构造函数、原型、实例。 
        // 面向对象中还有一个很重要的概念:继承。
        // 继承在JS代码中,指的是类与类之间的关系.

        // 人类 
        // function People(name, age, sex) {
        //     this.name = name;
        //     this.age = age;
        //     this.sex = sex;
        // }
        // People.prototype.sayHello = function() {
        //     console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
        // }
        // var p = new People("小红", 13, '女');


        // // // 小孩类
        // function Child(name, age, sex, height, weight) {
        //     //构造函数式继承
        //     People.call(this, name, age, sex);
        //     this.height = height;
        //     this.weight = weight;
        // }
        // // Child想要复用People的方法 那么就需要能够访问到People的原型 
        // // 方式1 直接把Child的原型更改为People的原型
        // //原型继承
        // Child.prototype = People.prototype;
        // // 补回constructor属性
        // Child.prototype.constructor = Child;
        // // 添加新的方法
        // Child.prototype.jump = function() {
        //     console.log('jump~~~~')
        // }
        // var c = new Child("小明", 12, "男", 150, 100);

        // c.sayHello();
        // c.jump();
        // p.jump();
        // 结论: 方式1  给子类添加方法之后 影响到了父类

        // 分析: 因为方式1 是将子类的原型与父类的原型设置为同一个地址 所以添加的时候会给该地址添加属性和方法 那么就会影响到父类的实例 那么有没有这样的一个对象同时符合: 1 是一个对象 2 能够访问父类的方法 3 改变的时候不会影响父类  结果发现:父类的实例符合这三个条件




        // 方式2
        function People(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        People.prototype.sayHello = function() {
            console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
        }
        var p = new People("小红", 13, '女');


        // 小孩类
        function Child(name, age, sex, height, weight) {
            People.call(this, name, age, sex);
            this.height = height;
            this.weight = weight;
        }

        Child.prototype = new People(); //采用这种继承方式  可以实现继承 但是child实例中里面有多余的属性
        // 补回 constructor
        Child.prototype.constructor = Child;
        // 添加新方法 
        Child.prototype.jump = function() {
            console.log(" U jump I jump");
        }

        var c1 = new Child("小明", 12, "男", 150, 100);
        c1.sayHello();
        c1.jump();
        p.sayHello();
    </script>
</body>

</html>

2.1使用object.create();

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 寄生式继承
        function People(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        People.prototype.sayHello = function() {
            console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
        }



        // 小孩类
        function Child(name, age, sex, height, weight) {
            People.call(this, name, age, sex);
            this.height = height;
            this.weight = weight;
        }
        Child.prototype = Object.create(People.prototype);

        var c = new Child("小明", 12, "男", 150, 100);
        c.sayHello();
        //这么写也不会多出多余的属性
        // Object.create 作用是根据一个对象作为原型 生成一个实例
    </script>
</body>

</html>

2.寄生式继承

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 构造函数执行的四步: 
        // 1 开辟一个新的空间
        // 2 与函数中的this绑定
        // 3 执行函数的代码
        // 4 返回this

        // 寄生式继承
        function People(name, age, sex) {
            this.name = name;
            this.age = age;
            this.sex = sex;
        }
        People.prototype.sayHello = function() {
            console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
        }

        // 小孩类
        function Child(name, age, sex, height, weight) {
            People.call(this, name, age, sex);
            this.height = height;
            this.weight = weight;
        }

        // 实现继承
        function extend(father, child, methods) {
            // 定义一个空构造函数
            function F() {};
            // 将该构造函数的原型指向父类的原型
            F.prototype = father.prototype;
            // 将子类的原型指向这个空构造函数的实例 注:该实例没有自身属性 但是可以调用父类的方法
            child.prototype = new F();
            // 补回
            child.prototype.constructor = child;
            // 给子类添加新的属于子类自己的方法
            for (var i in methods) {
                child.prototype[i] = methods[i];
            }
        }


        extend(People, Child, {
            jump: function() {
                console.log("u jump i jump")
            }
        })

        var c1 = new Child("小明", 13, "男", 160, 200);
        var p = new People("小红", 13, '女');
        c1.sayHello();
        c1.jump();
        p.sayHello();
    </script>
</body>

</html>

3.ES6继承

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 构造函数执行的四步: 
        // 1 开辟一个新的空间
        // 2 与函数中的this绑定
        // 3 执行函数的代码
        // 4 返回this
        class People {
            constructor(name, age, sex) {
                this.name = name;
                this.age = age;
                this.sex = sex;
            }
            sayHello() {
                console.log("大家好,我是" + this.name + ",我今年" + this.age + "岁, 我是" + this.sex + "的")
            }
        }

        class Child extends People {
            constructor(name, age, sex, height, weight) {
                // super必须写在第一条
                super(name, age, sex);
                this.height = height;
                this.weight = weight;
            }
            jump() {
                console.log("u jump i jump")
            }
        }



        var c1 = new Child("小明", 13, "男", 160, 200);
        var p = new People("小红", 13, '女');
        c1.sayHello();
        c1.jump();
        p.sayHello();
    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值