码迷,mamicode.com
首页 > Web开发 > 详细

JS构造map/dict结构

时间:2014-11-04 15:25:28      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   for   sp   on   cti   size   new   

function Map() {


var struct = function(key, value) {

this.key = key;

this.value = value;

}


var put = function(key, value) {

for (var i = 0; i < this.arr.length; i++) {

if (this.arr[i].key === key) {

this.arr[i].value = value;

return;

}

}

this.arr[this.arr.length] = new struct(key, value);

}


var get = function(key) {

for (var i = 0; i < this.arr.length; i++) {

if (this.arr[i].key === key) {

return this.arr[i].value;

}

}

return null;

}


var remove = function(key) {

var v;

for (var i = 0; i < this.arr.length; i++) {

v = this.arr.pop();

if (v.key === key) {

continue;

}

this.arr.unshift(v);

}

}


var size = function() {

return this.arr.length;

}


var isEmpty = function() {

return this.arr.length <= 0;

}

this.arr = new Array();

this.get = get;

this.put = put;

this.remove = remove;

this.size = size;

this.isEmpty = isEmpty;

}


JS构造map/dict结构

标签:style   io   ar   for   sp   on   cti   size   new   

原文地址:http://my.oschina.net/crazyharry/blog/340526

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!