现在的位置: 主页 > 新闻中心 > 文章列表

javascript 实现 Map 工具包

作者:厦工楚胜(湖北)专用汽车制造有限公司 来源:www.szzq168.com 发布时间:2017-09-03 12:55:45
 

javascript 实现 Map 工具包 [javascript]

(function() {

window['XR'] = {};

var midContainer = new Array();

var mapContainer = new Array();

var MAPID = 0;

function Map(mid) {

var type = typeof (mid);

if ((type != "string") && (type != "number"))

throw "Map id must be a string or number!";

for (var _c = 0; midContainer[_c]; _c++)

if (mid == midContainer[_c])

throw "You have already created Map : " + mid;

var identify = MAPID++;

midContainer[identify] = mid;

mapContainer[identify] = {};

mapContainer[identify]["id"] = mid;

this.id = mid;

this.prefix = "K_";

this.toString = function()

return "This is a map object!";

}

Map.prototype.getMapId = function()

return this.id;

Map.prototype.getMapIndex = function() {

var index = -1;

for (var _i = 0; mapContainer[_i]; _i++) {

if (this.id == mapContainer[_i]["id"])

index = _i;

}

return index;

}

Map.prototype.put = function(key, value) {

if ( typeof (key) != "string")

throw "key must be a string!";

if ( typeof (value) == "function")

throw "value shouldn't be a function!";

if (this.trimStr(key) == "")

throw "key is empty!";

var index = -1;

index = this.getMapIndex();

if (index != -1)

key = this.prefix + key;

mapContainer[index][key] = value;

}

Map.prototype.get = function(key) {

var index = -1;

index = this.getMapIndex();

var value = "";

if (index != -1)

var _tV = mapContainer[index];

key = this.prefix + key;

value = (_tV.hasOwnProperty(key)) ? _tV[key] : "You haven't save this key's value!";

else

value = "Current Map has lost connection!";

return value;

}

Map.prototype.deleteKey = function(key) {

var index = -1;

index = this.getMapIndex();

key = this.prefix + key;

var _tV = mapContainer[index];

if (_tV.hasOwnProperty(key))

delete _tV[key];

}

Map.prototype.clearMap = function() {

var index = -1;

index = this.getMapIndex();

var maxId = MAPID - 1;

if (index <= maxId) {

for (var t = index; t < maxId; t++)

mapContainer[t] = mapContainer[t + 1];

midContainer[t] = midContainer[t + 1];

mapContainer[maxId] = null;

midContainer[maxId] = null;

this.id = null;

this.toString = null;

MAPID--;

}

}

Map.prototype.trimStr = function(str) (\s*$)/g, "");

Map.prototype.isEmpty = function() {

var index = -1;

index = this.getMapIndex();

if (index != -1) {

for (var attr in mapContainer[index]) {

//alert(mapContainer[index][attr]);

if (attr != "id")

return false;

}

}

return true;

}

Map.prototype.showMap = function() {

var index = -1;

index = this.getMapIndex();

var str = "";

if (this.id != null) {

str = "Map:\t" + this.id + "\n";

for (var attr in mapContainer[index]) {

if (attr != "id")

str += attr + ":\t" + mapContainer[index][attr] + "\n";

}

} else

str = "This Map doesn't exist!";

alert(str);

return str;

}

window['XR']['Map'] = Map;

})()

//TEST CODES------------

function test() {

with (XR)

var m1 = new Map("ddd");

m1.put("dd", "dfdfdf");

m1.put("dd2", "8yeah!");

//alert(m1.get("dd"));

//alert(m1.isEmpty());

m1.deleteKey("dd");

m1.clearMap();

m1.showMap();

}

test();

//-----------------

企业建站2800元起,携手武汉肥猫科技,做一个有见地的颜值派!更多优惠请戳:天门网站建设 http://tianmen.45qun.com

  • 上一篇:python学习(3)- 深入流程控制
  • 下一篇:最后一页