面向对象的继承方式(汇总)

第一种: 原型链的方式

       function Parent(name){
            this.name=name;
        }
        Parent.prototype.say=function(){
            console.log('saying');
        }
        function Child(name,age){
            this.name = name;
            this.age=age;
        }
        Child.prototype = new Parent();
        Child.prototype.constructor=Child;//修复
        var c1=  new Child('Tom',18);
        console.log(c1);
        console.log(c1.name,c1.age);
        console.log(Child.prototype.constructor);

第二种: 借用构造函数

        function Parent(name) {
            this.name =name;
        }

        function Child(name,age) {
            Parent.call(this);
            this.age = age;
        }

        var c1=  new Child('Tom',18);
        console.log(c1.name,c1.age);

第三种: 组合继承

       function Parent(name) {
            this.name = name;
            this.walk=function(){
                console.log('waking~~~');
            }
        }
        Parent.prototype.say = function () {
            console.log('saying~~~');
        }
        function Child(name, age) {
            Parent.call(this,name,age);
            this.age = age;
        }
        Child.prototype = new Parent();
        Child.prototype.constructor = Child;//修复
        var c1 = new Child('Tom', 18);
        console.log(c1.name, c1.age);
        c1.say()

第四种: 寄生式组合继承   ----最完美的继承的方法

       function Parent(name) {
            this.name = name;
            this.walk = function () {
                console.log('waking~~~');
            }
        }
        Parent.prototype.say = function () {
            console.log('saying~~~');
        }
        function Child(name, age) {
            Parent.call(this, name, age);
            this.age = age;
        }
        Child.prototype = Object.create(Child.prototype);
        Child.prototype.constructor = Child;//修复
        //在这里增加新方法
        var c1 = new Child('Tom', 18);
        console.log(c1.name, c1.age);
        c1.say()

第五种: 寄生式继承  ---了解即可

        function createAnother(original) {
            var clone = Object.create(original);    //通过调用函数创建一个新对象
            clone.sayHi = function () {               //以某种方式来增强这个对象
                alert("Hi");
            };

            return clone;                        //返回这个对象
        }
        
        var person = {
            name: "Bob",
            friends: ["Shelby", "Court", "Van"]
        };
        var anotherPerson = createAnother(person);
        anotherPerson.sayHi();
       // anotherPerson是基于person创建的一个新对象,新对象不仅具有person的所有属性和方法,还有自己的sayHi()方法。

相关推荐

  1. 面向对象继承方式汇总

    2023-12-12 19:32:02       40 阅读
  2. 面向对象方法)、私有化、继承、多态

    2023-12-12 19:32:02       35 阅读
  3. 面向对象继承

    2023-12-12 19:32:02       30 阅读
  4. 面向对象进阶-继承

    2023-12-12 19:32:02       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-12 19:32:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-12 19:32:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-12 19:32:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-12 19:32:02       18 阅读

热门阅读

  1. C语言 数组指针

    2023-12-12 19:32:02       39 阅读
  2. SpringBoot中实现跨域的几种常用方式

    2023-12-12 19:32:02       31 阅读
  3. gitea仓库迁移

    2023-12-12 19:32:02       39 阅读
  4. 区块链:改变世界的技术

    2023-12-12 19:32:02       42 阅读
  5. Docker中安装并配置阿里巴巴的Sentinel控制台

    2023-12-12 19:32:02       40 阅读
  6. MySQL_6.MySQL常用创建语句

    2023-12-12 19:32:02       40 阅读
  7. 翻页新篇章:从Offset/Limit到游标分页的全面探索

    2023-12-12 19:32:02       36 阅读
  8. TypeScript 第四节:运算符

    2023-12-12 19:32:02       37 阅读
  9. 12、vue3(十二):用户管理页面搭建

    2023-12-12 19:32:02       37 阅读
  10. 定义一个存活态 HTTP 请求接口

    2023-12-12 19:32:02       37 阅读