[分享]用JavaScript摹拟VB中的 Collection 集合,纯属好玩,欢迎BT

KimSoft
博客专家认证
2006-03-28 05:39:21
集合的用处我就不说了,摹拟VB中的 Collection 集合

方法
count
add (item, key, before, after)
item(index_or_key)
remove(index_or_key)
removeAt(index)

<SCRIPT LANGUAGE="JavaScript">
<!--
/**
* Collection 摹拟VB中的 Collection 集合
* @version build 20060328
* @author KimSoft (jinqinghua [at] gmail.com)
* @update
*/

function Collection() {
var _array = [[]]; //data be stored as array like this:
/*
[
[["item1"]],
[["item2", "key1"]],
[["item3", 2]]
]
*/

/**
* 返回 Long(长整数),包含集合中的对象数目。只读。
*/
Collection.prototype.count = function(){
return _array[0].length;
};

/**
* 添加一个成员到 Collection 对象。
* @param item 必需的。任意类型的表达式,指定要添加到集合中的成员。
* @param key 可选的。唯一字符串表达式,指定可以使用的键字符串,代替位置索引来访问集合中的成员。
* @param before 可选的。表达式,指定集合中的相对位置。
* @param after 可选。表达式,指定集合中的相对位置。
可以指定 before 位置或 after 位置,但不能同时指定这两个位置。
* @error before 或 after 参数是字符串表达式或数值表达式,均须引用集合中现有成员,否则将导致错误发生。
如果指定的 key 和集合中现有成员的 key 发生重复,则也会导致错误发生。

*/
Collection.prototype.add = function (item, key, before, after){
if (before != null && after != null){
throw new Error("parameter before and after can not be specified at the same time!");
}
if (before == null && after == null){
_array[0][_array[0].length] = new Array(item, key);
} else {
if (before != null){
//to do
}
if (after != null){
//to do
}
}
};

/**
* 利用位置或键返回 Collection 对象的指定成员。
* @param index 成员位置
* @error 如果 index 的值与集合的现有成员不匹配,则会导致错误发生
*/
Collection.prototype.item = function (index) {
if (_array[0][index]){
return _array[0][index][0];
}
for (var i = 0; i < _array[0].length; i++) {
if (_array[0][i][1] == index){
return _array[0][i][0];
}
}
throw new Error("index or key \"" + index + "\" not found!");
};

/**
* 根据成员位置把成员从 Collection 对象中删除。
* @param index 成员位置
* @error 如果 index 的值与集合的现有成员不匹配,则会导致错误发生
*/
Collection.prototype.removeAt = function (index){
if (_array[0][index]){
_array[0].splice(index, 1);
} else {
throw new Error("index \"" + index + "\" not found!");
}
};

/**
* 把成员从 Collection 对象中删除。
* @param index 位置或键
* @error 如果 index 的值与集合的现有成员不匹配,则会导致错误发生
*/
Collection.prototype.remove = function (index) {
if (_array[0][index]){
this.removeAt(index);
} else {
for (var i = 0; i < _array[0].length; i++) {
if (_array[0][i][1] == index){
this.removeAt(i)
}
}
throw new Error("key \"" + index + "\" not found!");
}
};
}

var collection = new Collection();
document.write(collection.count());
collection.add("item0");
collection.add("item1", "key1");
collection.add("item2", 2);
document.write(collection.item("key1"));
/*
document.write(collection.count());
collection.remove(2);
document.write(collection.count());
*/
//-->
</SCRIPT>
...全文
257 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
KimSoft 2006-04-04
  • 打赏
  • 举报
回复
Scripting.Dictionary对象你在页面创建时有时会报安全警告。上面的只是一个轻量级的。
baifanSailing 2006-04-04
  • 打赏
  • 举报
回复
老大,如果只打算在IE里用的话,有现成的:
http://www.microsoft.com/china/vbscript/vbslang/vsobjDictionary.htm
hbhbhbhbhb1021 2006-03-29
  • 打赏
  • 举报
回复
学铭轩
-------------------------------


MARK
mingxuan3000 2006-03-29
  • 打赏
  • 举报
回复
还有两个todo没有完成,

都放出来 ,需要的时候就方便了
hbhbhbhbhb1021 2006-03-29
  • 打赏
  • 举报
回复
好东东
hbhbhbhbhb1021 2006-03-29
  • 打赏
  • 举报
回复
javascript版越来越不好玩了。
--------------------------------------
呵呵,要给人看的时间呀!
要不然怎么BT呀
KimSoft 2006-03-29
  • 打赏
  • 举报
回复
还有两个todo没有完成,不过已经够用了。但可能还有问题。
mingxuan3000 2006-03-29
  • 打赏
  • 举报
回复
好东西呀
sky0120 2006-03-29
  • 打赏
  • 举报
回复
UP
是是非非 2006-03-28
  • 打赏
  • 举报
回复
yiyioo 2006-03-28
  • 打赏
  • 举报
回复
我来捧场~````````````
KimSoft 2006-03-28
  • 打赏
  • 举报
回复
javascript版越来越不好玩了。

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧