JScript版CollectionBase类的一个实现

博客提到JScript虽有集合对象Array,但接口调用规则不适合.NET Framework。作者制作了JScript版的CollectionBase类,整理集合操作并实现相关方法。使用该类制作Menu、Tree等组件时可继承,省去重复代码。

    集合是我们最常用的数据结构了,JScript虽然给我们提供了一个内部类集合对象Array,可是Array的接口调用规则并不适合我们已经习惯了的.NET Framework。为了在JScript中能方便的使用集合,我找葫芦画瓢作了一个JScript版的的CollectionBase类。

    没有什么复杂的东西,只是把集合需要的操作整理了一下,实现了以下方法:

None.gif None.gif function  CollectionBase(){}
None.gifNone.gifCollectionBase.prototype.Clear 
=   function ()
None.gifNone.gifCollectionBase.prototype.Clone 
=   function ()
None.gifNone.gifCollectionBase.prototype.Item 
=   function (index)
None.gifNone.gifCollectionBase.prototype.Add 
=   function (item)
None.gifNone.gifCollectionBase.prototype.Contains 
=   function (item)
None.gifNone.gifCollectionBase.prototype.IndexOf 
=   function (item)
None.gifNone.gifCollectionBase.prototype.LastIndexOf 
=   function (item)
None.gifNone.gifCollectionBase.prototype.Insert 
=   function (item)
None.gifNone.gifCollectionBase.prototype.InsertAt 
=   function (item, index)
None.gifNone.gifCollectionBase.prototype.Remove 
=   function (item)
None.gifNone.gifCollectionBase.prototype.RemoveAt 
=   function (index)
None.gifNone.gifCollectionBase.prototype.Swap 
=   function (itemA, itemB)
None.gifNone.gifCollectionBase.prototype.Sort 
=   function (sortCallback)
None.gifNone.gifCollectionBase.prototype.Reverse 
=   function ()
None.gif


    附CollectionBase源代码:

ExpandedBlockStart.gif ContractedBlock.gif < script  language ="javascript" > dot.gif
InBlock.gif
function CollectionBase()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.m_InnerArray = [];
InBlock.gif    
this.m_Count = 0;
InBlock.gif
InBlock.gif    
this.toString = function()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
return '[class CollectionBase]';
ExpandedSubBlockEnd.gif    }
;
ExpandedSubBlockEnd.gif}

InBlock.gif
InBlock.gifCollectionBase.prototype.Clear 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.m_InnerArray.splice(0this.m_Count);
InBlock.gif    
this.m_Count = 0;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Clone 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var cb = new CollectionBase();
InBlock.gif    cb.m_InnerArray 
= this.m_InnerArray.slice(0);
InBlock.gif    cb.m_Count 
= this.m_Count;
InBlock.gif    
return cb;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Item 
= function(index)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
return this.m_InnerArray[index];
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Add 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.Insert(item);
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Contains 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
return (this.IndexOf(itme) != -1);
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.IndexOf 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
for ( var i=0 ; i < this.m_Count ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
if ( this.m_InnerArray[i] == item )
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif              
return i;
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif    }

InBlock.gif    
return -1;
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.LastIndexOf 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
for ( var i=this.m_Count-1 ; i >= 0 ; --i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
if ( this.m_InnerArray[i] == item )
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif             
return i;
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Insert 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.InsertAt(item, this.m_Count);
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.InsertAt 
= function(item, index)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
if ( typeof(item) != 'undefined' && typeof(index) != 'undefined' )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
throw 'you must override this mothed.';
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//* general case code */
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//*
InBlock.gif    item.m_Collection = this;
InBlock.gif    if ( item.m_ChildCollection )
InBlock.gif    {
InBlock.gif         if ( this.Contains(item.m_ChildCollection) )
InBlock.gif         {
InBlock.gif             item.m_ChildCollection = null;
InBlock.gif         }
InBlock.gif         item.m_ChildCollection.m_ParentCollection = item;
InBlock.gif    }
InBlock.gif    this.m_Items.splice(index, 0, item);
InBlock.gif    this.m_Invalidate = true; 
ExpandedSubBlockEnd.gif    
*/

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Remove 
= function(item)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
for ( var i=0 ; i < this.m_Count ; ++i )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
if ( this.m_InnerArray[i] == item )
ExpandedSubBlockStart.gifContractedSubBlock.gif         
dot.gif{
InBlock.gif             
this.RemoveAt(i);
InBlock.gif             
break;
ExpandedSubBlockEnd.gif         }

ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.RemoveAt 
= function(index)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
if ( this.m_InnerArray[index] )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
this.m_InnerArray.splice(index, 1);
InBlock.gif         
this.m_Count--;
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Swap 
= function(itemA, itemB)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
var iPsnA = this.IndexOf(itemA);
InBlock.gif    
var iPsnB = this.IndexOf(itemB);
InBlock.gif     
InBlock.gif    
if ( iPsnA != -1 && iPsnB != -1 )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
this.m_InnerArray[iPsnA] = itemB;
InBlock.gif         
this.m_InnerArray[iPsnB] = itmeA;
ExpandedSubBlockEnd.gif    }
 
ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Sort 
= function(sortCallback)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
if ( sortCallback )
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
this.m_InnerArray.sort(sortCallback);
ExpandedSubBlockEnd.gif    }

InBlock.gif    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif         
this.m_InnerArray.sort();
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif}
;
InBlock.gif
InBlock.gifCollectionBase.prototype.Reverse 
= function()
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif    
this.m_InnerArray.reverse();
ExpandedBlockEnd.gif}
;
None.gif</
< SPAN>script>


    有了这个CollectionBase类以后,我们在制作需要集合为主要数据结构的Menu, Tree, Grid, ToolBar等组件时,就可以直接继承CollectionBase,从而为我们省去很多的重复代码。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值