继承方式

1、对象冒充

//对象冒充对应构造函数方式
//对象冒充对应构造函数方式
function ClassA(sColor){
this.color=sColor;
this.showColor=function(){
alert(this.color);
};
}
function ClassB(sColor,sName){
this.newMethod=ClassA;
this.newMethod(sColor);
delete this.newMethod;

this.name=sName;
this.showName=function(){
alert(this.name);
};
}
var oa=new ClassA("RED");
var ob=new ClassB("blue","red");
oa.showColor();//RED
ob.showColor();//blue
ob.showName();//red

构造函数使用this关键字给所有属性和方法赋值(即采用类声明的构造函数方式)。因为构造函数只是一个函数,所以可使ClassA的构造函数成为ClassB的方法,然后调用它。ClassB就会收到ClassA的构造函数中定义的属性和方法。

2、call()方法是与经典的对象冒充方法最相似的方法。它的第一个参数用作this的对象。其他参数都都直接传递给函数自身。其实就相当于前一个是对象,后一个是普通的参数一样。

function showColor(sPrefix,sSuffix){
alert(sPrefix+this.color+sSuffix);
};
var obj=new Object();
obj.color="red";
//the color is red ,a very nice color indeed
showColor.call(obj,"the color is "," ,a very nice color indeed");
function ClassA(sColor){
this.color=sColor;
this.showColor=function(){
alert(this.color);
};
}
function ClassB(sColor,sName){
ClassA.call(this,sColor);
this.name=sName;
this.showName=function(){
alert(this.name);
};
}
var ob=new ClassB("red","ooo");
ob.showColor(); //red
ob.showName();//ooo

3、apply()方法有两个参数,用作this的对象和要传递给函数的参数的数组。

function showColor(sPrefix,sSuffix){
alert(sPrefix+this.color+sSuffix);
};
var obj=new Object();
obj.color="green";
//the color is green ,a very nice color indeed
showColor.apply(obj,new Array("the color is "," ,a very nice color indeed"));
function ClassA(sColor){
this.color=sColor;
this.showColor=function(){
alert(this.color);
};
}
function ClassB(sColor,sName){
ClassA.apply(this,arguments);
this.name=sName;
this.showName=function(){
alert(this.name);
};
}
var ob=new ClassB("red","ooo");
ob.showColor();//red
ob.showName();//ooo

4、原型链
原型链扩展了类的原型定义方式。prototype对象是个模板,要实例化的对象都以这个模板为基础。prototype对象的任何属性和方法都被传递给那个类的所有实例。原型链利用这种功能来实现继承机制。原型链的弊端是不支持多重继承。记住,原型链会用另一种类型的对象重写类的prototype属性。因此子类的所有属性和方法必须出现在prototype属性被赋值以后。

//原型链方式对应原型方式
function ClassA(){
}
ClassA.prototype.color="red";
ClassA.prototype.showColor=function(){
alert(this.color);
};
function ClassB(){
}
ClassB.prototype=new ClassA();
ClassB.prototype.name="redred";
ClassB.prototype.showName=function(){
alert(this.name);
};
var objA=new ClassA();
var objB=new ClassB();
objA.color="blue";
objA.showColor();//blue
objB.color="green";
objB.name="dodo";
objB.showColor();//green
objB.showName();//dodo
alert(objB instanceof ClassA);//true
alert(objB instanceof ClassB);//true

5、混合方式
与定义类时同样的方式,即利用构造函数方式定义属性,用原型方式定义方法。而在继承机制中,可以用对象冒充继承构造函数的属性,用原型链继承prototype对象的方法。

function ClassA(sColor){
this.color=sColor;
}
ClassA.prototype.showColor=function(){
alert(this.color);
};
function ClassB(sColor,sName){
ClassA.call(this,sColor);
this.name=sName;
}
ClassB.prototype=new ClassA();
ClassB.prototype.showName=function(){
alert(this.name);
};
var objA=new ClassA("BLUE");
objA.showColor();//BLUE
var objB=new ClassB("red","baba");
objB.showColor();//red
objB.showName();//baba
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值