Javascript 封装map、list

警告
本文最后更新于 2022-06-24,文中内容可能已过时。

当年收藏的一个封装脚本

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 * Util.$(objid):获取指定ID的dom对象
 *
 * Util.ie:判断浏览器是否为IE
 *
 * List类:模拟java的List,支持iterator
 *   add(obj):向List的最后追加一个对象
 *   addat(index, obj):在List的index位置插入一个对象
 *   get(index):在List获取index位置的对象
 *   set(index, obj):在List的替换index位置的对象
 *   size():获取List的对象个数
 *   remove(index):移除List中index位置的对象
 *   clear():清空List
 *   iterator():获取到List的迭代对象
 *
 * Map类:模拟java的Map,支持直接对value的iterator
 *   put(key, value):在Map的添加一个key value的键值对
 *   get(key):在Map获取键为key的值
 *   remove(key):溢出Map中键为key的对象
 *   containsKey(key):判断Map中是否存在指定key的对象
 *   keySet():获取Map中所有的键的List
 *   values():获取Map中所有的值的List
 *   size():获取List的对象个数
 *   clear():清空List
 *   iterator():获取到List的迭代对象
 *
 * Iterator用法:for(var it = list.iterator(); it.hasNext();) { var item = it.next(); }
 *
 * Url类:获取url中参数的值,使用var u = new Url(location.search); var value = url.getvalue(para);即可得到
 *
 * EventHandler类:封装事件方法,可以为事件方法的调用添加参数,屏蔽了浏览器差异
 *   EventHandler.createEvent(func, ...args):创建事件对象,args为参数值,需要与func的参数对应,不需要传递event对象
 *   EventHandler.getEvent():在事件的处理方法中获取当前的event对象
 *   EventHandler.getElement(e):获取触发event事件的dom对象
 *   EventHandler.attachEvent(obj, eventname, func):为obj添加eventname事件,事件处理方法为func
 *   EventHandler.detachEvent(obj, eventname, func):为obj注销eventname事件的func方法
 *
 * EventType类:封装了客户端的大多数事件类型,屏蔽了浏览器差异
 * 使用方式:EventHandler.attachEvent(obj, EventType.click, func)
 * 目前封装了click, rclick, mousedown, mousemove, mouseup, mouseover, mouseout, scroll, focus, blur, change, keypress, keydown, keyup, submit
 */

/*
 * 修改记录
 */

function Util() {}
Util.$ = function (objid) {
  return document.getElementById(objid);
};
//浏览器的判断,true为IE,false为Firefox
Util.ie = navigator.appName.indexOf("Microsoft") != -1 ? true : false;

function Iterator(iteratorArray) {
  this.itArr = iteratorArray;
  this.index = -1;
}

Iterator.prototype = {
  hasNext: function () {
    if (this.index + 1 >= this.itArr.length) {
      return false;
    } else {
      return true;
    }
  },

  next: function () {
    this.index++;
    return this.itArr[this.index];
  },
};

function Map() {
  this.arr = new Array();
}

Map.prototype = {
  put: function (key, value) {
    if (!this.containsKey(key)) {
      this.arr.push([key, value]);
    } else {
      for (var i = 0; i < this.arr.length; i++) {
        if (this.arr[i][0] == key) {
          this.arr[i][1] = value;
          return;
        }
      }
    }
  },

  get: function (key) {
    for (var i = 0; i < this.arr.length; i++) {
      if (this.arr[i][0] == key) {
        return this.arr[i][1];
      }
    }
    return null;
  },

  remove: function (key) {
    for (var i = 0; i < this.arr.length; i++) {
      if (this.arr[i][0] == key) {
        this.arr.splice(i, 1);
        return;
      }
    }
  },

  containsKey: function (key) {
    for (var i = 0; i < this.arr.length; i++) {
      if (this.arr[i][0] == key) {
        return true;
      }
    }
    return false;
  },

  keySet: function () {
    var l = new List();
    for (var i = 0; i < this.arr.length; i++) {
      l.add(this.arr[i][0]);
    }
    return l;
  },

  values: function () {
    var l = new List();
    for (var i = 0; i < this.arr.length; i++) {
      l.add(this.arr[i][1]);
    }
    return l;
  },

  size: function () {
    return this.arr.length;
  },

  clear: function () {
    this.arr = [];
  },

  iterator: function () {
    var vs = new Array();
    for (var i = 0; i < this.arr.length; i++) {
      vs.push(this.arr[i][1]);
    }
    var it = new Iterator(vs);
    return it;
  },
};

function List() {
  this.arr = new Array();
}

List.prototype = {
  add: function (obj) {
    this.arr.push(obj);
  },

  addat: function (index, obj) {
    this.arr.splice(index, 0, obj);
  },

  get: function (index) {
    return this.arr[index];
  },

  set: function (index, obj) {
    this.arr.splice(index, 1, obj);
  },

  size: function () {
    return this.arr.length;
  },

  remove: function (index) {
    this.arr.splice(index, 1);
  },

  clear: function () {
    this.arr = [];
  },

  iterator: function () {
    var it = new Iterator(this.arr);
    return it;
  },
};

function Url(urlstr) {
  this.paraMap = new Map();

  if (urlstr.indexOf("?") > -1) {
    urlstr = urlstr.substr(1);
  }
  if (urlstr.indexOf("&") > -1) {
    var pvarr = urlstr.split("&");
    for (var i = 0; i < pvarr.length; i++) {
      var pv = pvarr[i].split("=");
      this.paraMap.put(pv[0], pv[1]);
    }
  } else {
    var pv = urlstr.split("=");
    this.paraMap.put(pv[0], pv[1]);
  }
}

Url.prototype = {
  getvalue: function (para) {
    return this.paraMap.get(para);
  },
};

function EventType() {}

//鼠标单击
EventType.click = Util.ie ? "onclick" : "click";
EventType.rclick = Util.ie ? "oncontextmenu" : "contextmenu";
EventType.mousedown = Util.ie ? "onmousedown" : "mousedown";
EventType.mousemove = Util.ie ? "onmousemove" : "mousemove";
EventType.mouseup = Util.ie ? "onmouseup" : "mouseup";
EventType.mouseover = Util.ie ? "onmouseover" : "mouseover";
EventType.mouseout = Util.ie ? "onmouseout" : "mouseout";
EventType.scroll = Util.ie ? "onscroll" : "scroll";
EventType.focus = Util.ie ? "onfocus" : "focus";
EventType.blur = Util.ie ? "onblur" : "blur";
EventType.change = Util.ie ? "onchange" : "change";
EventType.keypress = Util.ie ? "onkeypress" : "keypress";
EventType.keydown = Util.ie ? "onkeydown" : "keydown";
EventType.keyup = Util.ie ? "onkeyup" : "keyup";
EventType.submit = Util.ie ? "onsubmit" : "submit";

function EventHandler() {}

//创建事件对象
EventHandler.createEvent = function (func) {
  var argarr = [];
  for (var i = 1; i < arguments.length; i++) argarr.push(arguments[i]);
  return function () {
    func.apply(window, argarr);
  };
};

//获取事件的句柄
EventHandler.getEvent = function () {
  if (Util.ie) {
    return window.event;
  } else {
    var ev = null;
    if (EventHandler.getEvent.caller.caller)
      ev = EventHandler.getEvent.caller.caller.arguments[0];
    return ev;
  }
};

EventHandler.getElement = function (e) {
  var ev = e;
  if (!ev) {
    if (Util.ie) {
      return window.event;
    } else {
      if (EventHandler.getEvent.caller.caller)
        ev = EventHandler.getEvent.caller.caller.arguments[0];
      return ev;
    }
  }
  return Util.ie ? ev.srcElement : ev.target;
};

//添加事件
EventHandler.attachEvent = function (obj, eventname, func) {
  if (Util.ie) {
    obj.attachEvent(eventname, func);
  } else {
    obj.addEventListener(eventname, func, true);
  }
};

//注销事件
EventHandler.detachEvent = function (obj, eventname, func) {
  if (Util.ie) {
    obj.detachEvent(eventname, func);
  } else {
    obj.removeEventListener(eventname, func, true);
  }
};
0%