1. 父对象
function classA(sColor){
this.color=sColor;
this.sayColor=function(){
alert(this.color);
};
}
2. 要继承的对象
function classB(sColor,sName){
//第一种写法
//this.newMethod=classA;
//this.newMethod(sColor);
//delete this.newMethod;
// 第二种写法
//classA.call(this,sColor);
// 第三种写法
//classA.apply(this,arguments);
this.name=sName;
this.sayName=function(){
alert(this.name);
};
}
3. 对象测试
var objA=new classA("red");
var objB=new classB("blue","you");
objA.sayColor();
objB.sayColor();
objB.sayName();
4. 另外一种继承方法
function classA1(){}
classA1.prototype.color="red1";
classA1.prototype.sayColor=function(){
alert(this.color);
};
function classB1(){}
classB1.prototype=new classA1();
var objB1=new classB1();
objB1.sayColor();