ES5|ES6继承

继承
原型继承

优点: 如果Student继承了Human,那么Student的实例对象不单有Student定义的属性和方法,还将拥有Human的属性和方法,Student的原型prototype上的属性,将会保留在实例对象的 __proto__上,也就是原型上属性和方法就是实例对象上的属性和方法

Student.prototype = new Human();这样的话所有的Student的实例都将拥有Student.prototype原型上的属性和方法,也就是Human实例的属性和方法,s1是student的实例,他继承以后应该用于name,age,banji,score属性和sayAge,sayScore方法

缺点: 无法给父类的参数传参

function Human(name,age){
    this.name = name;
    this.age = age;
    this.sayAge = function(){
        console.log('我的年龄是:'+this.age)
    }
} 

//h1作为Human的实例,他将拥有Human上定义的说有属性和方法
var h1 = new Human('lili',12);

function Student(banji,score){
    this.banji = banji;
    this.score = score;
    this.sayScore = function(){
        console.log('我今天考试得分是:'+this.score)
    }
}
//把新的对象作为Student的原型
Student.prototype = h1;
//s1作为Student的实例,他将拥有Student上定义的说有属性和方法
var s1 = new Student('2003',60);        
console.log(s1);
借用构造函数继承

Student的实例拥有父类Human上定义的所有属性和方法,使用借用父构造函数的方法实现继承,只能继承构造函数上定义的属性和方法,不能继承到原型链

function Human(name,age){
    this.name = name;
    this.age = age;
    this.sayAge = function(){
        console.log('我的年龄是:'+this.age)
    }
} 
Human.prototype.a = "11";
Human.prototype.cc = function(){
    console.log('cc')
};
function Student(banji,score,name,age){
    //this的指向问题,此时Human前面没有new,他当做普通函数执行,普通函数里面this指向window
    //call和apply同样可以调用函数,并且在调用的时候可以改变this的指向
    Human.call(this,name,age);
    this.banji = banji;
    this.score = score;
    this.sayScore = function(){
        console.log('我今天考试得分是:'+this.score)
    }
}

var s1 = new Student('2003',65,'李磊',12);
console.dir(s1);
console.log(window)
组合继承

子构造函数的实例继承父构造函数定义的所有属性和方法包括原型上的(通过原型继承)

可以给父构造函数传参(借用构造函数)

组合继承 = 借用构造函数 + 通过原型继承;

 function Human(name,age){
     this.name = name;
     this.age = age;
     this.sayAge = function(){
         console.log('我的年龄是:'+this.age)
     }
 } 

function Student(banji,score,name,age){
    //1 借用构造函数
    Human.call(this,name,age)
    this.banji = banji;
    this.score = score;
    this.sayScore = function(){
        console.log('我今天考试得分是:'+this.score)
    }
}
//2 原型继承
Student.prototype = new Human("lili",12);//new Human()的参数任意

var s1 = new Student('2003',56,"李四",12);
console.log(s1)
es6继承
class Human{
    constructor(name , age){
        this.name = name;
        this.age = age;
    }
    sayName(){
        console.log(`我的名字是${this.name}`);
    }
}

class People extends Human{
    constructor(name , age ,score , clas){
        super(name , age);
        this.score = score;
        this.clas = clas;
    }
    sayHello(){
        console.log(`hello 我的名字是${this.name}我今年${this.age}岁,我在${this.clas}班,我考了${this.score}分`);
    }
}
var stu = new People('jack' , 12 , 100 , '2003');
console.log(stu);
stu.sayHello();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值