<!-- ————————JS对象之继承 ———————— -->
//父类
function Sup(name){
this.name=name;
}
//父类的原型
Sup.prototype={
constructor:Sup,
sayName:function(){
alert(this.name);
}
};
//子类构造函数
function Sub(age){
this.age=age;
}
//让子类的原型等于父类的实例
Sub.prototype=new Sup("A");
var sub=new Sub();
alert(sub.name);
<!-- ————————JS对象之继承(原型继承) ———————— -->
//原型继承的特点:既继承了父类的模版又继承了父类的原型对象
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.id=0;
function Boy(sex){
this.sex=sex;
}
//继承已经实现的
Boy.prototype=new Person("A",120);
var b=new Boy("男");
alert(b.id);//0
<!-- ————————JS对象之继承(类继承) ———————— -->
//类继承的特点:只继承模版 不继承原型对象
function Person2(name,age){//父类
this.name=name;
this.age=age;
}
Person2.prototype.id=100;
function Boy2(name,age,sex){//子类
Person2.call(this,name,age);
this.sex=sex;
}
var boy2=new Boy2("B",20,"男");
alert(boy2.name);
boy2.id;//
<!-- ————————JS对象之继承(原型继承+构造函数继承) ———————— -->
function Person3(name,age){//父类
this.name=name;
this.age=age;
}
Person3.prototype.id=1000;
Person3.prototype.sayname=function(){
alert(this.name);
}
function Boy3(name,age,sex){//子类
Person2.call(this,name,age);//借用构造函数继承
this.sex=sex;
}
Boy3.prototype=new Person3();
var boy3=new Boy3("C3",20,"男");
alert(boy3.name);
alert(boy3.id);//