JavaScript中⽤数组实现键值对
原⽂地址为:
转⾃:
vbs中有dictionary对象,js中也有相应的对象,可js的dictionary对象不是跨浏览器的,所以⽤数组实现了键值对的功能。
  今天写浏览器端js程序,需要⽤到键值对的功能,vbs中有dictionary对象,js中也应该有对应的dictionary对象,查了⼀下js⼿册,js 中果然有dictionary对象,程序写好了,跨浏览器⼀测试,发现只有IE⽀持new ActiveXObject("Scripting.Dictionary"),其他浏览器或许有兼容写法,⽀持dictionary对象,⽹上搜索了⼀下,没有搜到,突然想到js的数组,查了⼀些资料,实现了我想要的功能,跨浏览器测试⼀下,⾕歌、⽕狐、opera都兼容,以下是程序代码,参考了⽹上的⼀个例⼦,稍作了下修改:
<script type="text/javascript" language="javascript">
//⾃定义字典对象
function Dictionary(){
this.data = new Array();
this.put = function(key,value){
this.data[key] = value;
};
< = function(key){
return this.data[key];
};
this.data[key] = null;
};
this.isEmpty = function(){
return this.data.length == 0;
};
this.size = function(){
javascript全局数组return this.data.length;
};
}
//使⽤例⼦
var d = new Dictionary();
d.put("CN", "China");
d.put("US", "America");
document.("CN"));
</script>
转载请注明本⽂地址:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。