js对象继承

继承是单向的

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

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()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值