JavaScript的另外两种继承机制
1>zInherit:
它是一个组件,用来继承基类的所有属性和方法。跟以前说到的原型链模式非常类似,只不过比原型更安全,也无须考虑参数问题。下面看看zInherit的用法:
该组件中只有两个方法:inheritfrom() instanceof()
function Polygon(iSides){
this.iSides = iSides;
if(typeof Polygon._initialized == "undefined"){
Polygon.prototype.getArea = function(){
return 0;
}
Polygon._initialized = true;
}
}
function Traingle(iHeight,iWidth){
Polygon.call(this,3);
this.iHeight = iHeight;
this.iWidth = iWidth;
if(typeof Traingle._initialized == "undefined"){
Traingle.inheritfrom(Polygon);
//上行的效果跟原来的这个非常类似
// Traingle.prototype = new Polygon();
Traingle.prototype.getArea = function(){
return 0.5*iHeight*iWidth;
}
Traingle._initialized = true;
}
}
var traingle = new Traingle(12,4);
alert(traingle.getArea());//24
Note:在执行前必须加载zInherit.js
2>xbObjects.js
它是一个功能更强大的继承机制,除了可以继承基类的属性和方法外,还可以调用父类的方法,在执行前页必须增加组件xbObject.js.
具体的步骤如下:
第一步:注册类(_classes.registerClass("sub_class","base_class") _classes.register("sub_class"))
第二步:构造函数,并在函数中定义类(_classes.defineClass("class_name",prototypeFunction))
第三步:执行init()方法,该方法的参数必须跟构造函数的参数匹配
第四步:可以通过parentMehtod("method_name",arguments)
<mce:script type="text/javascript"><!--
//Polygon
_classes.registerClass("Polygon"); //1
function Polygon(iSides){ //2
_classes.defineClass("Polygon",prototypeFunction);
this.init(iSides);//初始化Polygon的所有属性//3
function prototypeFunction(){//4
Polygon.prototype.init = function(iSides){
this.parentMethod("init");
this.iSides
相关文档:
1. document.write( " "); 输出语句
2.JS中的注释为//
3.传统的HTML文档顺序是:document- >html- >(head,body)
4.一个浏览器窗口中的DOM顺序是:window- >(navigator,screen,history,location,document)
5.得到表单中元素的名称和值:document.getElementById( "表单中元素的ID號 ").name(或valu ......
1. 标准的方法
<mce:script type="text/javascript"><!--
function openWin(src, width, height, showScroll){
window.showModalDialog (src,"","location:No;status:No;help:No;dialogWidth:"+width+";dialogHeight:"+height+";scroll:"+showScroll+";");
}
// --></mce:script> &n ......
var docEle = function() {
return document.getElementById(arguments[0]) || false;
}
function cloDiv(){
var overlayID="overlay";
var msgID = "overlayMsg";
document.body.removeChild(docEle(overlayID));
document.body.removeChild(docEle(msgID));
}
function openNewDiv() {
......
继承机制,说到继承,就要想到从基类开始,但是JavaScript中的类有两大类:一,内嵌的;二,用户自定义;一般前者不会被用来做基类,原因是为了保障js的安全。但是我们可以通过prototype来对基类进行扩充,增加我们想要的属性和方法。以下是自己对继承的几种方式的理解。
1> 对象 ......
keycode 0 =
keycode 1 =
keycode 2 =
keycode 3 =
keycode 4 =
keycode 5 =
keycode 6 =
keycode 7 =
keycode&nb ......