您的位置:首页 > Web前端 > JavaScript

JavaScript之继承方式

2012-03-17 23:06 211 查看
真正的面向对象语言必须支持继承机制,即一个类能够重用(继承)另一个类的方法和属性。
所有开发者定义的类都可作为基类,出于安全原因,本地类和宿主类不能作为基类。有时你可能想创建一个不能直接使用的基类,它只是用于给子类提供通用的函数,这种基数被看作抽象类。创建的子类将继承超类的所有属性和方法,包括构造函数及方法的实现。所有的属性和方法都是公用的,子类可直接访问这些方法,还可添加超类中没有的属性和方法,也可覆盖超类中的属性和方法。

一、 继承的方式

(1) 对象冒充:构造函数使用this关键字给所有属性和方法赋值(即采用声明的构造函数方式)。因为构造函数只是一个函数,所在可使ClassA的构造函数成为ClassB的方法,然后调用它,ClassB就会收到ClassA的构造函数中定义的属性和方法。所有的新属性和新方法都必须在删除了新方法的代码行后定义。

function ClassA(sColor){

this.color=sColor;

this.sayColor=function(){

alert(this.color);

}

}

function ClassB(sColor){

this.newMethod=ClassA;

this.newMethod(sColor);

delete this.newMethod //删除了对ClassAr的引用

this.name=sName;

this.sayName=function(){

alert(this.name);

}

}

var objA=new ClassA(“red”);

var objB=new ClassB(“blue”,”Nicholas”);

objA.sayColor(); //outputs “red”

objB.sayColor(); //outputs “blue”

objB.sayName(); //outputs “Nicholas”
对象冒充支持多重继承,一个类可以继承多个超类。

(2)、call()方法

它的第一个参数用作this的对象,其他参数都直接传递给函数自身。

function sayColor(sPrefix,sSuffix){

alert(sPrefix+this.color+sSuffix);

};

var obj=new Object();

obj.color=”red”;

syaColor.call(obj,”This color is”,”, a very nice color indeed.”);

//outpus “This color is red , a very nice color indeed.”
要与继承机制的对象冒充方法一起使用该方法,只需将前三行的赋值、调用和删除代码替换即可:

function ClassB(sColor,sName){

//this.newMethod=ClassA;

//this.newMethod(sColor);

ClassA.call(this,sColor);

this.name=sName;

this.sayName=function(){

alert(this.name);

};

}
(3)、apply()方法

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

function sayColor(sPrefix,sSuffix){

alert(sPrefix+this.color+sSuffix);

};

var obj=new Object();

obj.color=”red”;

sayColor.apply(obj,new Array(“The color is ”,”, a very nice color indeed.”));

//outputs “The color is red, a very nice color indeed.”
该方法也用于替换前三行的赋值、调用和删除新方法的代码。

function ClassB(sColor,sName){

//this.newMethod=ClassA;

//this.newMethod(sColor);

classA.apply(this,new Array(sColor));

this.name=sName;

this.sayName=function(){

alert(this.name);

};

}
(4)、原型链

prototype对象是个模版,要实例化的对象都以这个模板为基础。prototype对象的任何属性的方法都被传递给那个类的所有实例。

function ClassA(){

}

ClassA.prototype.color=”red”;

ClassA.prototype.sayColor=function(){

alert(this.color);

};

function ClassB(){

}

ClassB.prototype=new ClassA();

ClassB.prototype.name=””;

ClassB.prototype.sayName=function(){

alert(this.name);

}
调用ClassA的构造函数时,没有给它传递参数,这在原型链中是标准做法,要确保构造函数没有任何函数。子类的所有属性和方法都必须出现在prototype属性被赋值后,因为在它之前赋值的所有方法都会被删除。Prototype属性被替换成了新对象,添加了新方法的原始对象将销毁。原型链的弊端是不支持多重继承。

(5)、混合方式

用对象冒充继承构造函数属性,用原型链继承prototype对象的方法。

function ClassA(sColor){

this.color=sColor;

}

ClassA.prototype.sayColor=function(){

alert(this.color);

};

function ClassB(sColor,sName){

ClassA.call(this,sColor);

this.name=sName;

}

ClassB.prototype=new ClassA();

ClassB.prototype.sayName=function(){

alert(this.name);

};
二、其它继承方式

由于ECMAScript继承机制的限制,世界各地的开发者都在为创建实现继承机制的其他方式而不懈地努力着。简单介绍一个其中两种方式。

(1)、zlnherit

利用zlnherit库,不必须使用原型链,也可实现继承。支持多重继承,原型链不支持这种能力。Zlnherit给Object类添加了两上方法:inheritFrom()和instanceOf()。

inheritFrom()方法指定类的所有方法,接受一个参数,即要复制的方法所属的类。必须在原型赋值之处调用inheritFrom()方法。要继承属性和方法,inheritFrom()方法必须与对象冒充一起使用。

instanceOf()方法是instanceof运算符的替代器。

(2)、xbObjects

xbObjects的目的是为JavaScript提供更强的面向对象范型,不仅支持继承,还支持方法的重载和调用超类方法的能力。

第一步:必须注册类,此时需定义它是由哪个类继承而来。其必须放在指定子类的前面。

第二步:在构造函数内调用defineClass()方法,传给它类名及被Clary称为原型函数的指针,该函数用于初始化对象的所有属性和方法。

第三步:为该类创建init()方法,该方法负责设置该类的所有属性,必须接受与构造函数相同的参数。

_classes.registerClass(“ClassA”);

function ClassA(sColor){

_classes.defineclass(“classA”,prototypeFunction);

this.init(sColor);

function prototypeFunction(){

ClassA.prototype.init=function(sColor){

this.parentMethod(“init”);

this.color=sColor;

} ;

};

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: