ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

JS高级----对象的继承

2021-11-22 21:04:13  阅读:151  来源: 互联网

标签:console log Doctor 继承 JS ---- Person constructor prototype



什么是继承?

一个对象继承另一个对象,可以使用父级对象的属性和方法,共享资源,避免大量浪费系统资源


prototype 属性的作用:

  • 原型对象的所有属性和方法,都能被实例对象共享。

  • 如果属性和方法定义在原型上,那么所有实例对象就能共享,不仅节省了内存,还体现了实例对象之间的联系。

        function Person() {

        }
        Person.prototype.eyes = 2;
        p1 = new Person();

        console.log(p1.eyes);

在这里插入图片描述


原型链:

  • 所有对象都有自己的原型对象
  • 原型对象也是对象,它也有自己的原型
  • 对象的原型最终都可以上溯到Object.prototype
  • Object.prototype的原型为null,原型链的尽头就是Object.prototype

prototype指向上一级原型对象

        function Person() {

        }

        function Student() {

        }

        p1 = new Person();
        s1 = new Student();
        console.log(Person.prototype);
        console.log(Student.prototype);

在这里插入图片描述


constructor 属性:

  • prototype对象都有constructor属性,指向prototype对象所在的构造函数
  • constructor属性可以知道某一个实例对象来自于哪个函数
     function Person() {

        }
        console.log(Person.prototype);
        console.log(Person.prototype.constructor);
        console.log(Person.prototype.constructor === Person);

在这里插入图片描述


构造函数的继承:

  1. 使用call()方法继承:
        function Person() {
            this.eyes = 2;
            this.legs = 2;
        }

        function Doctor() {
            Person.call(this);
        }
        // 如果直接子类的原型 = 父类的原型 子类的原型还是为Object
        // Doctor.prototype = Person.prototype

        Doctor.prototype = Object.create(Person.prototype);
        Doctor.prototype.constructor = Doctor;
        d1 = new Doctor();

        console.log(d1.eyes);
        // 要修改子类的prototype指向父类的原型 
        // 如果不把子类的原型指向父类的原型  子类的prototype还是指向Object
        // 但是可以调用父类的方法
        console.log(Person.prototype);
        console.log(Doctor.prototype);
        // 还要修改子类的prototype对象的constructor属性的指向
        console.log(Person.prototype.constructor);
        console.log(Doctor.prototype.constructor);

在这里插入图片描述

  1. 子类的prototype对象 = new 父类(); 继承
      function Person() {
            this.eyes = 2;
            this.legs = 2;
            this.study = function() {
                console.log('学习');

            }
        }

        function Doctor() {


        }

        // 子类的prototype对象  =  父类的一个对象
        Doctor.prototype = new Person();
        // 修改子类prototype对象的constructor属性为自身
        Doctor.prototype.constructor = Doctor;
        d1 = new Doctor();
        console.log(d1.eyes);
        d1.study();
        console.log(Person.prototype);
        console.log(Doctor.prototype);
        console.log(Person.prototype.constructor);
        console.log(Doctor.prototype.constructor);

在这里插入图片描述

标签:console,log,Doctor,继承,JS,----,Person,constructor,prototype
来源: https://blog.csdn.net/bjsyc123456/article/details/121479264

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有