- //Array的扩展
- /**
- * 返回一个对象在Array中的位置
- */
- Array.prototype.indexOf = function(obj) {
- var result = -1;
- for(var i = 0; i < this.length; i++) {
- if(this[i] == obj) {
- result = i;
- break;
- }
- }
- return result;
- }
- /**
- * 检查一个对象是否包含在Array中
- */
- Array.prototype.contains = function(obj) {
- return this.indexOf(obj) > -1;
- }
- /**
- * 添加一个对象
- */
- Array.prototype.add = function(obj, nodup) {
- if(!(this.contains(obj) && nodup)) {
- this[this.length] = obj;
- }
- }
- /**
- * 删除一个对象
- */
- Array.prototype.remove = function(obj) {
- if(this.contains(obj)) {
- var index = this.indexOf(obj);
- for(var i = index; i < this.length - 1; i++) {
- this[i] = this[i + 1];
- }
- this.length--;
- }
- }
//map 的扩展
- /**
- * Map的构造函数
- */
- function Map(){
- this.container = new Object();
- }
- /**
- * 添加一个键-值
- */
- Map.prototype.put = function(key, value){
- this.container[key] = value;
- }
- /**
- * 通过键获取一个值
- */
- Map.prototype.get = function(key){
- return this.container[key];
- }
- /**
- * 返回该Map对象的键集合
- */
- Map.prototype.keySet = function() {
- var keyset = new Array();
- var count = 0;
- for (var key in this.container) {
- // 跳过object的extend函数
- if (key == 'extend')
- continue;
- keyset[count] = key;
- count++;
- }
- return keyset;
- }
- /**
- * 返回该Map对象的大小
- */
- Map.prototype.size = function() {
- var count = 0;
- for (var key in this.container) {
- // 跳过object的extend函数
- if (key == 'extend')
- continue;
- count++;
- }
- return count;
- }
- /**
- * 返回该Map对象的字符串形式
- */
- Map.prototype.toString = function(){
- var str = "";
- for (var i = 0, keys = this.keySet(), len = keys.length; i < len; i++) {
- str = str + keys[i] + "=" + this.container[keys[i]] + ";/n";
- }
- return str;
- }