ICode9

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

js对象继承

2021-09-25 09:05:14  阅读:104  来源: 互联网

标签:console name Person 继承 age js 对象 Student prototype


继承是单向的

继承是指子类继承了父类的属性和方法

1.1对象冒充继承

子类、派生类

父类、超类、基类

实现方法:在子类的构造函数中调用父类的构造函数,通过call或apply改变父类中的this指向,进而完成属性的继承

通过遍历父类的原型,完成方法继承

 //人
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        //方法
        Person.prototype.say = function () {
            console.log(this.name + " 在哈哈大笑");
        }


        //学生对象
        function Student(no, name, age) {
            this.no = no;
            //属性继承
            // Person.call(this, name, age);//对象冒充
            Person.apply(this,[name,age])
        }

        // Student.prototype.say=function(){
        //     console.log(this.name+" 在哈哈大笑");
        // }
        // Student.prototype = Person.prototype;
        // console.log(Person.prototype);
        for(var i in Person.prototype){ //遍历人的原型,赋值给学生的原型
            Student.prototype[i]=Person.prototype[i];
            // console.log(i);
        }

        Student.prototype.study = function () {
            console.log(this.name + " 在奋笔疾书....");
        }



        var s = new Student("1001", '小明', 16);
        console.log(s);
        // s.study();
        // s.say();//未继承

        //实现 学生继承 人

        var p=new Person('光头强',19);
        console.log(p);

1.2原型链继承

将父类的对象实例赋值子类的原型。

原型链继承的查找路线:对象实例--->对象的构造函数----->对象的原型----->父类实例------>父类构造函数---->父类的原型

  //人
        function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        //方法
        Person.prototype.say = function () {
            console.log(this.name + " 在哈哈大笑");
        }


        //学生对象
        function Student(no, name, age) {
            this.no=no;
        }

        //将父类的对象实例赋值给子类的原型对象
        Student.prototype = new Person();//原型链继承


        Student.prototype.study = function () {
            console.log(this.name + " 在奋笔疾书....");
        }

        // var s = new Student();
        // console.log(s);
        // s.say()
        var s = new Student(1000,'小刚',6);
        console.log(s);
        s.say()

总结:原型链继承可以较为完美的实现方法的继承,但是对于属性继承不够理想

1.3组合继承

组合继承:使用原型链继承完成方法的继承;使用对象冒充完成属性的继承

 //人
         function Person(name, age) {
            this.name = name;
            this.age = age;
        }

        //方法
        Person.prototype.say = function () {
            console.log(this.name + " 在哈哈大笑");
        }


        //学生对象
        function Student(no, name, age) {
            this.no=no;
            Person.call(this,name,age);//对象冒充
        }

        //将父类的对象实例赋值给子类的原型对象
        Student.prototype = new Person();//原型链继承

        Student.prototype.study = function () {
            console.log(this.name + " 在奋笔疾书....");
        }
    
        var s = new Student(1000,'小刚',6);
        console.log(s);
        s.say()

标签:console,name,Person,继承,age,js,对象,Student,prototype
来源: https://blog.csdn.net/nanbei___/article/details/120467033

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

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

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

ICode9版权所有