function Father(name){
this.name=name;
this.colors=["red","blue"];
}
Father.prototype.sayName=function(){alert(this.name);}; //prototype为每个函数都有的属性,用他来定义公共方法,每个实例都可以共用
function Son(name,age){
Father.call(this,name); //继承属性
this.age=age; //子类属性
}
Son.prototype=new Father(); //继承方法
Son.prototype.sayAge=function(){
alert(this.age);
}
this.name=name;
this.colors=["red","blue"];
}
Father.prototype.sayName=function(){alert(this.name);}; //prototype为每个函数都有的属性,用他来定义公共方法,每个实例都可以共用
function Son(name,age){
Father.call(this,name); //继承属性
this.age=age; //子类属性
}
Son.prototype=new Father(); //继承方法
Son.prototype.sayAge=function(){
alert(this.age);
}
本文介绍了一种使用JavaScript实现的继承模式,通过构造函数Father和Son演示了如何在一个子类中调用父类的方法并继承父类的属性及方法。
647

被折叠的 条评论
为什么被折叠?



