继承(原型链继承、冒充继承、混合继承)

本文介绍了JavaScript中三种继承方式:原型链继承、冒充继承和混合继承。通过示例代码详细解释了每种方式的实现原理和优缺点,帮助读者理解JavaScript对象间的继承关系。
摘要由CSDN通过智能技术生成

<script>
    //人类  父类
    function Human(name,age,height){
        this.name=name
        this.age=age
        this.height=height
    }
    Human.prototype.sleep=function(){
        console.log(this.name+"正在睡觉")
    }
    Human.prototype.shopping=function(){
        console.log(this.name+"正在购物")
    }
    Human.prototype.run=function(){
        console.log(this.name+"正在奔跑")
    }
    //1.原型链继承
    //学生类  子类
    // function Student(name,age,height){
        
    // }
    // Student.prototype=new Human("小周",20,"158")
    // Student.prototype.study=function(){
    //     console.log(this.name+"正在学习")
    // }l
    // Student.prototype.play=function(){
    //     console.log(this.name+"正在玩")
    // }
    // var s1=new Student()
    // console.log(s1)
    // console.log(s1.age)
    // s1.shopping()    
 //    //教师类   子类
    // function Teacher(name,age,height){
        
    // }
    // Teacher.prototype=new Human("陆老师","42","164")
    // Teacher.prototype.teach=function(){
    //     console.log(this.name+"正在教课")
    // }
    // Teacher.prototype.correct=function(){
    //     console.log(this.name+"正批改作业")
    // }
    // var t1=new Teacher()
    // console.log(t1)
    // console.log(t1.height)
    // t1.sleep()
    
    //2.冒充继承
    //学生类
    function Student(name,age,height){
        Human.call(this,name,age,height)
    }
    Student.prototype.study=function(){
        console.log(this.name+"正在学习")
    }
    Student.prototype.play=function(){
        console.log(this.name+"正在玩")
    }
    var s2=new Student("小刘","21","185")
    console.log(s2)
    //教师类
    function Teacher(name,age,height){
        Human.call(this,name,age,height)
    }
    Teacher.prototype.teach=function(){
        console.log(this.name+"正在教课")
    }
    Teacher.prototype.correct=function(){
        console.log(this.name+"正批改作业")
    }
    var t2=new Teacher("王老师","32","176")
    console.log(t2)
    
    //3.混合继承
    //学生类
    function Student(name,age,height){
        Human.call(this,name,age,height)
    }
    Student.prototype=new Human()
    Student.prototype.study=function(){
        console.log(this.name+"正在学习")
    }
    Student.prototype.play=function(){
        console.log(this.name+"正在玩")
    }
    var s3=new Student("小马","16","180")
    console.log(s3)
    console.log(s3.name)
    s3.run()
    //教师类
    function Teacher(name,age,height){
        Human.call(this,name,age,height)
    }
    Teacher.prototype=new Human()
    Teacher.prototype.teach=function(){
        console.log(this.name+"正在教课")
    }
    Teacher.prototype.correct=function(){
        console.log(this.name+"正批改作业")
    }
    var t3=new Teacher("赵老师","28","166")
    console.log(t3)
    console.log(t3.age)
    t3.sleep()
    

    // 1.原型链继承: 
    //     优点:将父类对象赋值给子类原型上,让父级对象作为子集的原型 
    //        缺点:所有属性相同,无法对父级属性进行初始化
    // 2.冒充继承
    //     优点:通过call或者apply更改对应的this指向完成继承,可以更改其this指向,指向参数对象
    //        缺点:冒充继承无法继承 原型中的方法
    // 2.混合继承
    //     优点:通过冒充继承来继承对应属性 ,通过原型继承继承对应方法
    //        缺点:无
    
</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值