js写的一个简单的hashtable
2009-01-08 16:44
351 查看
js写的一个简单的hashtable,主要用来客户端添加url参数,如param1=value1¶m2=value2¶m3=value3。
有一些js框架也有类似的方法,如有名的prototype框架,而且其还提供所有表单元素,直接serialize成url参数形式。很久前用过
一段时间,不过现在一般用jQuery。废话少说,贡献出来,和大家交流与批评指正。
/*
* create by hjp
* blog:http://jackhuclan.cnblogs.com
* 2009-01-08
* usage for append or remove parameters to url
*/
var HashTable = function sg_HashTable()
{
this.__hash=new ActiveXObject("Scripting.Dictionary");
this.Items=[];
this.__Keys=null;
this.__Values=null;
}
HashTable.prototype={
add:function(key,value)
{
if(this.__hash.Exists(key))
{
this.__hash.Remove(key);
var pos=this.find(key);
if(pos!=-1)
{
this.Items[pos].Value=value;
}
this.__hash.Add(key,value);
}
else
{
this.__hash.Add(key,value);
this.Items.push({"Key":key,"Value":value});
}
},
remove:function(key)
{
if(this.__hash.Exists(key))
{
this.__hash.Remove(key);
var pos=this.find(key);
if(pos!=-1)
{
var newArr= this.Items.slice(0, pos).concat(this.Items.slice(pos + 1, this.Items.length));
delete this.Items;
this.Items=newArr;
}
}
},
removeAll:function()
{
this.__hash.RemoveAll();
delete this.Items;
this.Items=[];
},
getKeys:function(){
this.__Keys=[];
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
this.__Keys.push(item.Key);
}
return this.__Keys;
},
getValues:function(){
this.__Values=[];
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
this.__Values.push(item.Value);
}
return this.__Values;
},
toUrl:function()
{
var str="";
for(var i=0;i<this.Items.length-1;i++)
{
var item=this.Items[i];
if(encodeURIComponent)
str += encodeURIComponent(item.Key)+"="+encodeURIComponent(item.Value)+"&";
else
str += escape(item.Key)+"="+escape(item.Value)+"&";
}
var lastItem=this.Items[this.Items.length-1];
if(encodeURIComponent)
str += encodeURIComponent(lastItem.Key)+"="+encodeURIComponent(lastItem.Value);
else
str += escape(lastItem.Key)+"="+escape(lastItem.Value);
return str;
},
find:function(key)
{
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
if(item.Key==key)
{
return i;
}
}
return -1;
}
}
客户端调用如下:
<script src="sg_hashtable.js" type="text/javascript"></script>
<script type="text/javascript">
var hs=new HashTable();
hs.add("id1","1");
hs.add("id2","张三");
hs.add("id2","dddd");
hs.add("id4","4");
var str=hs.toUrl();
debugger
hs.getKeys();
hs.getValues();
hs.remove("id4");
debugger
hs.removeAll();
</script>
有一些js框架也有类似的方法,如有名的prototype框架,而且其还提供所有表单元素,直接serialize成url参数形式。很久前用过
一段时间,不过现在一般用jQuery。废话少说,贡献出来,和大家交流与批评指正。
/*
* create by hjp
* blog:http://jackhuclan.cnblogs.com
* 2009-01-08
* usage for append or remove parameters to url
*/
var HashTable = function sg_HashTable()
{
this.__hash=new ActiveXObject("Scripting.Dictionary");
this.Items=[];
this.__Keys=null;
this.__Values=null;
}
HashTable.prototype={
add:function(key,value)
{
if(this.__hash.Exists(key))
{
this.__hash.Remove(key);
var pos=this.find(key);
if(pos!=-1)
{
this.Items[pos].Value=value;
}
this.__hash.Add(key,value);
}
else
{
this.__hash.Add(key,value);
this.Items.push({"Key":key,"Value":value});
}
},
remove:function(key)
{
if(this.__hash.Exists(key))
{
this.__hash.Remove(key);
var pos=this.find(key);
if(pos!=-1)
{
var newArr= this.Items.slice(0, pos).concat(this.Items.slice(pos + 1, this.Items.length));
delete this.Items;
this.Items=newArr;
}
}
},
removeAll:function()
{
this.__hash.RemoveAll();
delete this.Items;
this.Items=[];
},
getKeys:function(){
this.__Keys=[];
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
this.__Keys.push(item.Key);
}
return this.__Keys;
},
getValues:function(){
this.__Values=[];
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
this.__Values.push(item.Value);
}
return this.__Values;
},
toUrl:function()
{
var str="";
for(var i=0;i<this.Items.length-1;i++)
{
var item=this.Items[i];
if(encodeURIComponent)
str += encodeURIComponent(item.Key)+"="+encodeURIComponent(item.Value)+"&";
else
str += escape(item.Key)+"="+escape(item.Value)+"&";
}
var lastItem=this.Items[this.Items.length-1];
if(encodeURIComponent)
str += encodeURIComponent(lastItem.Key)+"="+encodeURIComponent(lastItem.Value);
else
str += escape(lastItem.Key)+"="+escape(lastItem.Value);
return str;
},
find:function(key)
{
for(var i=0;i<this.Items.length;i++)
{
var item=this.Items[i];
if(item.Key==key)
{
return i;
}
}
return -1;
}
}
客户端调用如下:
<script src="sg_hashtable.js" type="text/javascript"></script>
<script type="text/javascript">
var hs=new HashTable();
hs.add("id1","1");
hs.add("id2","张三");
hs.add("id2","dddd");
hs.add("id4","4");
var str=hs.toUrl();
debugger
hs.getKeys();
hs.getValues();
hs.remove("id4");
debugger
hs.removeAll();
</script>
相关文章推荐
- 一个简单的js问题告诉你:你的经验到底值不值钱?
- 问题:关于贴友一个用js传递value默认值的简单实现
- node.js 一个简单的页面输出实现代码
- 使用原生js写的一个简单slider
- nodejs + express + ejs + mongodb 一个非常简单的前后端开发的实例3
- 用Vue.js实现一个简单的搜索框
- JS实现一个简单的抽奖系统
- 记一个简单的webpack.config.js
- [ckeditor系列]ckeditor 自己写的一个简单的image上传js 运用iframe的ajax上传
- d3.js学习4----做一个简单的图表
- JS模式:又一个简单的工厂模式
- 一个简单的二级联动效果,看完之后立马你也可以写一个(有源码,纯js,带注释)
- 第一个Three.js程序——动手写一个简单的场景
- 一个简单的动态加载js和css的jquery代码
- 使用Node.js + MongoDB实现一个简单的日志分析系统
- js写的一个简单的手风琴菜单
- icheck.js的一个简单demo
- 一个简单的js事件,循环table并自动计算总价
- 请写一个简单的幻灯效果页面,不使用JS来完成怎么做