实现继承的关键在于:子类必须拥有父类的全部属性和方法,同时子类还应该能定义自己特有的属性和方法
父类:
function People(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
}
People.prototype.sayHello=function(){
console.log('你好,我是'+this.name+'我今年'+this.age+'岁了')
}
People.prototype.sleep=function(){
console.log(this.name+'开始睡觉~~~')
}
子类:
function Student(name,age,sex,school,studentNumber){
this.name=name;
this.age=age;
this.sex=sex;
this.school=school;
this.studentNumber=studentNumber
}
//关键语句,实现继承
Student.prototype=new People();
Student.prototype.study=function(){
console.log(this.name+'正在学习')
}
Student.prototype.exam=function(){
console.log(this.name+'正在考试,加油')
}
//实例化
var hanmeimei=new Student('韩梅梅',9,'女','清华小学','100556')
hanmeimei.study()
hanmeimei.sayHello()