用call方法实现继承
function classA(sColor){
this.color=sColor;
this.sayColor=function(){
alert(this.color);
}
}
function classC(sColor,sName){
classA.call(this,sColor);
this.name=sName;
this.sayName=function(){
alert(this.name);
}
}
var obj1=new classC("red","jack");
obj1.sayColor();
obj1.sayName();
用apply方法实现继承
function classD(sColor,sName){
classA.apply(this,new Array(sColor));
this.name=sName;
this.sayName=function(){
alert(this.name);
}
}
var obj2=new classD("blue","sherry");
obj2.sayColor();
obj2.sayName();
构造函数,原型混合方法
function classA(sName){
this.name=sName;
}
classA.prototype.sayName=function(){
alert(this.name);
}
function classB(sName,sHeight){
classA.call(this,sName)
this.height=sHeight;
}
classB.prototype=new classA();
classB.prototype.sayHeight=function(){
alert(this.height);
}
var obj3=new classA("jack");
obj3.sayName();
var obj4=new classB("shrry",27);
obj4.sayName();
obj4.sayHeight();