javascript实现的java里的Map对象
2008-08-21 12:45
886 查看
javascript中,map的key转换成了js对象的属性名,value转换成了该属性的值,没有办法得到key这个对象
//自定义的Map 对象
function Map(){
this.elements=new Array();
this.size=function(){
return this.elements.length;
}
this.put=function(_key,_value){
this.elements.push({key:_key,value:_value});
}
this.remove=function(_key){
var bln=false;
try{
for (i=0;i<this.elements.length;i++){
if (this.elements[i].key==_key){
this.elements.splice(i,1);
return true;
}
}
}catch(e){
bln=false;
}
return bln;
}
this.containsKey=function(_key){
var bln=false;
try{
for (i=0;i<this.elements.length;i++){
中国网管联盟bitsCN.com
if (this.elements[i].key==_key){
bln=true;
}
}
}catch(e){
bln=false;
}
return bln;
}
this.get=function(_key){
try{
for (i=0;i<this.elements.length;i++){
if (this.elements[i].key==_key){
return this.elements[i];
}
}
}catch(e){
return null;
}
}
}
//测试Map的调用方法
function testMap(){
var testmap=new Map();
testmap.put("01","michael");
testmap.put("02","michael2");
alert (testmap.size());
var key="02"
if (testmap.containsKey(key)){
var element=testmap.get(key);
alert (element.key+"|"+element.value);
}else{
alert ("不包含"+key);
}
testmap.remove("02");
if (testmap.containsKey(key)){
var element=testmap.get(key);
alert (element.key+"|"+element.value);
}else{
alert ("不包含"+key);
}
}
//自定义的Map 对象
function Map(){
this.elements=new Array();
this.size=function(){
return this.elements.length;
}
this.put=function(_key,_value){
this.elements.push({key:_key,value:_value});
}
this.remove=function(_key){
var bln=false;
try{
for (i=0;i<this.elements.length;i++){
if (this.elements[i].key==_key){
this.elements.splice(i,1);
return true;
}
}
}catch(e){
bln=false;
}
return bln;
}
this.containsKey=function(_key){
var bln=false;
try{
for (i=0;i<this.elements.length;i++){
中国网管联盟bitsCN.com
if (this.elements[i].key==_key){
bln=true;
}
}
}catch(e){
bln=false;
}
return bln;
}
this.get=function(_key){
try{
for (i=0;i<this.elements.length;i++){
if (this.elements[i].key==_key){
return this.elements[i];
}
}
}catch(e){
return null;
}
}
}
//测试Map的调用方法
function testMap(){
var testmap=new Map();
testmap.put("01","michael");
testmap.put("02","michael2");
alert (testmap.size());
var key="02"
if (testmap.containsKey(key)){
var element=testmap.get(key);
alert (element.key+"|"+element.value);
}else{
alert ("不包含"+key);
}
testmap.remove("02");
if (testmap.containsKey(key)){
var element=testmap.get(key);
alert (element.key+"|"+element.value);
}else{
alert ("不包含"+key);
}
}
相关文章推荐
- javascript实现java的map对象,js实现new map()
- javascript实现的java里的Map对象
- javascript 实现的类似于java里的Map对象
- JavaScript中实现类似Java中的Map对象
- javascript 实现的类似于java里的Map对象
- javascript实现Java中的Map对象功能的实例详解
- javascript实现java的map对象,js实现new map()
- 在JavaScript中实现自己的Map对象
- JS 自定义对象实现Java中的Map对象功能
- Java程序员从笨鸟到菜鸟之(二十九)javascript对象的创建和继承实现
- 修改IE不兼容MAP()的问题,自定义实现JavaScript的Map对象
- javascript实现类似java中getClass()得到对象类名的方法
- JavaScript中模拟实现Java的集合类Map的常用方
- 在JavaScript中实现类似Java中List<Map>类型的数组
- JS 自定义对象实现Java中的Map对象功能
- Java程序员从笨鸟到菜鸟之(二十九)javascript对象的创建和继承实现
- 用JAVA编写浏览器内核之实现javascript的document对象与内置方法
- JavaScript实现Java中的Map结构
- JS自定义对象实现Java中Map对象功能的方法
- Java程序员从笨鸟到菜鸟之(二十九)javascript对象的创建和继承实现