1>---------
??? 前幾天做了一個 JScript版的CollecionBase類,用來解決需要使用集合作為主要數據結構的類的基類。不過當時挺忙的沒有給出繼承的示例,搞得有的網友對JavaScript繼承比較迷惑,于是今天使用四種方式來分別實現了4個ArrayList派生類。
??? 關于使用JavaScript進行面向對象編程(OOP),網上已有很多的文章說過了。這里我推薦兩篇文章大家看看,如果沒有理解怎么使用JavaScript的Function對象的prototype屬性來實現類定義及其原理,那么就仔細看看'面向對象的JavaScript編程'、'面向對象的Jscript'和'Classical Inheritance in JavaScript'哦(特別是第一篇及其相關討論的文章),否則后面一頭霧水不能怪我啦 。
??? 那個CollectionBase就不列了,第一個鏈接就是它了,下面是用四種方法實現的JavaScript'繼承'(以后就不打引號了,反正知道JavaScript的繼承不是經典OO里的繼承就行了):
??? 構造繼承法:  <script?language="javascript">
function?ArrayList01()
  {
????this.base?=?CollectionBase;
????this.base();?
????
????this.m_Array?=?this.m_InnerArray;
?????
????this.foo?=?function()
 ???? {
????????document.write(this?+?':?'?+?this.m_Count?+?':?'?+?this.m_Array?+?'<br?/>');
????};
???
????this.Add?=?function(item)
 ???? {
????????this.InsertAt(item,?this.m_Count);
????};
????
????this.InsertAt?=?function(item,?index)
 ???? {
????????this.m_InnerArray.splice(index,?0,?item);
????????this.m_Count++;
????};
???
????this.toString?=?function()
 ???? {
????????return?'[class?ArrayList01]';
????};
}
</script> ??? 原形繼承法:  <script?language="JavaScript">
function?ArrayList02()
  {???
????this.InsertAt?=?function(item,?index)
 ???? {
????????this.m_InnerArray.splice(index,?0,?item);
????????this.m_Count++;
????};
???
????this.m_Array?=?this.m_InnerArray;
?
????this.toString?=?function()
 ???? {
????????return?'[class?ArrayList02]';
????};
}
?
ArrayList02.prototype?=?new?CollectionBase();

ArrayList02.prototype.foo?=?function()
  {
?????document.write(this?+?':?'?+?this.m_Count?+?':?'?+?this.m_Array?+?'<br?/>');
};

ArrayList02.prototype.InsertAt?=?function(item,?index)
  {
????this.m_InnerArray.splice(index,?0,?item);
????this.m_Count++;
};
</script> ??? 實例繼承法:  <script?language="javascript">
function?ArrayList03()
  {
????var?base?=?new?CollectionBase();
???
????base.m_Array?=?base.m_InnerArray;
?????
????base.foo?=?function()
 ???? {
????????document.write(this?+?':?'+?this.m_Count?+?':?'?+?this.m_Array?+?'<br?/>');
????};
???
????base.InsertAt?=?function(item,?index)
 ???? {
????????this.m_InnerArray.splice(index,?0,?item);
????????this.m_Count++;
????};
???
????base.toString?=?function()
 ???? {
????????return?'[class?ArrayList03]';
????};
????return?base;
}
</script> ??? 附加繼承法:  <script?language="JavaScript">
function?ArrayList04()
  {
????this.base?=?new?CollectionBase();
????
????for?(?var?key?in?this.base?)
 ???? {
????????if?(?!this[key]?)
 ???????? {
????????????this[key]?=?this.base[key];
????????}?
????}
????
????this.m_Array?=?this.m_InnerArray;
?????
????this.InsertAt?=?function(item,?index)
 ???? {
????????this.m_InnerArray.splice(index,?0,?item);
????????this.m_Count++;
????};?
}

ArrayList04.prototype.foo?=?function()
  {
????document.write(this?+?':?'?+?this.m_Count?+?':?'?+?this.m_Array?+?'<br?/>');
}

ArrayList04.prototype.toString?=?function()
  {
????return?'[class?ArrayList04]';
}
</script> ??? 派生類中的foo是一個新增加的函數,用來輸出類的類型和m_InnerArray里的數據。toString()相當于override了CollectionBase中的toString(),不過其實就是賦值覆蓋,和'從JavaScript函數重名看其初始化方式'一文中說到的原理是一樣的。這個四種方法的原理和區別我稍候再作分析. 2>--------
|