集合是我们最常用的数据结构了,JScript虽然给我们提供了一个内部类集合对象Array,可是Array的接口调用规则并不适合我们已经习惯了的.NET Framework。为了在JScript中能方便的使用集合,我找葫芦画瓢作了一个JScript版的的CollectionBase类。
没有什么复杂的东西,只是把集合需要的操作整理了一下,实现了以下方法:
function
CollectionBase(){}
CollectionBase.prototype.Clear = function ()
CollectionBase.prototype.Clone = function ()
CollectionBase.prototype.Item = function (index)
CollectionBase.prototype.Add = function (item)
CollectionBase.prototype.Contains = function (item)
CollectionBase.prototype.IndexOf = function (item)
CollectionBase.prototype.LastIndexOf = function (item)
CollectionBase.prototype.Insert = function (item)
CollectionBase.prototype.InsertAt = function (item, index)
CollectionBase.prototype.Remove = function (item)
CollectionBase.prototype.RemoveAt = function (index)
CollectionBase.prototype.Swap = function (itemA, itemB)
CollectionBase.prototype.Sort = function (sortCallback)
CollectionBase.prototype.Reverse = function ()
CollectionBase.prototype.Clear = function ()
CollectionBase.prototype.Clone = function ()
CollectionBase.prototype.Item = function (index)
CollectionBase.prototype.Add = function (item)
CollectionBase.prototype.Contains = function (item)
CollectionBase.prototype.IndexOf = function (item)
CollectionBase.prototype.LastIndexOf = function (item)
CollectionBase.prototype.Insert = function (item)
CollectionBase.prototype.InsertAt = function (item, index)
CollectionBase.prototype.Remove = function (item)
CollectionBase.prototype.RemoveAt = function (index)
CollectionBase.prototype.Swap = function (itemA, itemB)
CollectionBase.prototype.Sort = function (sortCallback)
CollectionBase.prototype.Reverse = function ()
附CollectionBase源代码:
<
script
language
="javascript"
>
function CollectionBase()
{
this.m_InnerArray = [];
this.m_Count = 0;
this.toString = function()
{
return '[class CollectionBase]';
};
}
CollectionBase.prototype.Clear = function()
{
this.m_InnerArray.splice(0, this.m_Count);
this.m_Count = 0;
};
CollectionBase.prototype.Clone = function()
{
var cb = new CollectionBase();
cb.m_InnerArray = this.m_InnerArray.slice(0);
cb.m_Count = this.m_Count;
return cb;
};
CollectionBase.prototype.Item = function(index)
{
return this.m_InnerArray[index];
};
CollectionBase.prototype.Add = function(item)
{
this.Insert(item);
};
CollectionBase.prototype.Contains = function(item)
{
return (this.IndexOf(itme) != -1);
};
CollectionBase.prototype.IndexOf = function(item)
{
for ( var i=0 ; i < this.m_Count ; ++i )
{
if ( this.m_InnerArray[i] == item )
{
return i;
}
}
return -1;
};
CollectionBase.prototype.LastIndexOf = function(item)
{
for ( var i=this.m_Count-1 ; i >= 0 ; --i )
{
if ( this.m_InnerArray[i] == item )
{
return i;
}
}
};
CollectionBase.prototype.Insert = function(item)
{
this.InsertAt(item, this.m_Count);
};
CollectionBase.prototype.InsertAt = function(item, index)
{
if ( typeof(item) != 'undefined' && typeof(index) != 'undefined' )
{
throw 'you must override this mothed.';
}
/**//* general case code */
/**//*
item.m_Collection = this;
if ( item.m_ChildCollection )
{
if ( this.Contains(item.m_ChildCollection) )
{
item.m_ChildCollection = null;
}
item.m_ChildCollection.m_ParentCollection = item;
}
this.m_Items.splice(index, 0, item);
this.m_Invalidate = true;
*/
};
CollectionBase.prototype.Remove = function(item)
{
for ( var i=0 ; i < this.m_Count ; ++i )
{
if ( this.m_InnerArray[i] == item )
{
this.RemoveAt(i);
break;
}
}
};
CollectionBase.prototype.RemoveAt = function(index)
{
if ( this.m_InnerArray[index] )
{
this.m_InnerArray.splice(index, 1);
this.m_Count--;
}
};
CollectionBase.prototype.Swap = function(itemA, itemB)
{
var iPsnA = this.IndexOf(itemA);
var iPsnB = this.IndexOf(itemB);
if ( iPsnA != -1 && iPsnB != -1 )
{
this.m_InnerArray[iPsnA] = itemB;
this.m_InnerArray[iPsnB] = itmeA;
}
};
CollectionBase.prototype.Sort = function(sortCallback)
{
if ( sortCallback )
{
this.m_InnerArray.sort(sortCallback);
}
else
{
this.m_InnerArray.sort();
}
};
CollectionBase.prototype.Reverse = function()
{
this.m_InnerArray.reverse();
};
</ script>
function CollectionBase()
{
this.m_InnerArray = [];
this.m_Count = 0;
this.toString = function()
{
return '[class CollectionBase]';
};
}
CollectionBase.prototype.Clear = function()
{
this.m_InnerArray.splice(0, this.m_Count);
this.m_Count = 0;
};
CollectionBase.prototype.Clone = function()
{
var cb = new CollectionBase();
cb.m_InnerArray = this.m_InnerArray.slice(0);
cb.m_Count = this.m_Count;
return cb;
};
CollectionBase.prototype.Item = function(index)
{
return this.m_InnerArray[index];
};
CollectionBase.prototype.Add = function(item)
{
this.Insert(item);
};
CollectionBase.prototype.Contains = function(item)
{
return (this.IndexOf(itme) != -1);
};
CollectionBase.prototype.IndexOf = function(item)
{
for ( var i=0 ; i < this.m_Count ; ++i )
{
if ( this.m_InnerArray[i] == item )
{
return i;
}
}
return -1;
};
CollectionBase.prototype.LastIndexOf = function(item)
{
for ( var i=this.m_Count-1 ; i >= 0 ; --i )
{
if ( this.m_InnerArray[i] == item )
{
return i;
}
}
};
CollectionBase.prototype.Insert = function(item)
{
this.InsertAt(item, this.m_Count);
};
CollectionBase.prototype.InsertAt = function(item, index)
{
if ( typeof(item) != 'undefined' && typeof(index) != 'undefined' )
{
throw 'you must override this mothed.';
}
/**//* general case code */
/**//*
item.m_Collection = this;
if ( item.m_ChildCollection )
{
if ( this.Contains(item.m_ChildCollection) )
{
item.m_ChildCollection = null;
}
item.m_ChildCollection.m_ParentCollection = item;
}
this.m_Items.splice(index, 0, item);
this.m_Invalidate = true;
*/
};
CollectionBase.prototype.Remove = function(item)
{
for ( var i=0 ; i < this.m_Count ; ++i )
{
if ( this.m_InnerArray[i] == item )
{
this.RemoveAt(i);
break;
}
}
};
CollectionBase.prototype.RemoveAt = function(index)
{
if ( this.m_InnerArray[index] )
{
this.m_InnerArray.splice(index, 1);
this.m_Count--;
}
};
CollectionBase.prototype.Swap = function(itemA, itemB)
{
var iPsnA = this.IndexOf(itemA);
var iPsnB = this.IndexOf(itemB);
if ( iPsnA != -1 && iPsnB != -1 )
{
this.m_InnerArray[iPsnA] = itemB;
this.m_InnerArray[iPsnB] = itmeA;
}
};
CollectionBase.prototype.Sort = function(sortCallback)
{
if ( sortCallback )
{
this.m_InnerArray.sort(sortCallback);
}
else
{
this.m_InnerArray.sort();
}
};
CollectionBase.prototype.Reverse = function()
{
this.m_InnerArray.reverse();
};
</ script>
有了这个CollectionBase类以后,我们在制作需要集合为主要数据结构的Menu, Tree, Grid, ToolBar等组件时,就可以直接继承CollectionBase,从而为我们省去很多的重复代码。