//---------------------------------------------------------------------- // Map function Map(){ this.key = []; this.value = {}; } Map.prototype.put = function(k, v){ if (this.value[k] == null) this.key.push(k); this.value[k] = v; }; Map.prototype.get = function(k){ return this.value[k]; }; Map.prototype.remove = function(k){ delete this.value[k]; }; Map.prototype.size = function(){ return this.key.length; }; Map.prototype.toString = function(){ var str = ""; for (var prop in this.value) { str += "key: " + prop + "; value: " + this.value[prop] + "<br />"; } return str; }; Map.prototype.containsKey = function(k){ if (this.value[k] !== undefined) return true; return false; }; Map.prototype.keyToArray = function(){ var k = []; var i = 0; for (k[i++] in this.value) ; return k; }; Map.prototype.valueToArray = function(){ var v = []; var i = 0; for (var prop in this.value) v[i++] = this.value[prop]; return v; }; //---------------------------------------------------------------------- // 检测浏览器 // 调用check.browser(); // one function B(){ } B.b = function(){ for (var i in navigator) { document.write("navigator." + i + " = " + navigator[i] + "<br />"); } }; // two var check = { browser: function() { for (var i in navigator) document.write("navigator." + i + " = " + navigator[i] + "<br />"); } }; var Brower={ // 判断是否为IE6浏览器 isIE6:function(){return navigator.userAgent.search('MSIE')>0&&navigator.userAgent.search('6')>0;}, // 判断是否为IE浏览器 isIE:function(){return navigator.userAgent.search('MSIE')>0;}, // 判断是否为Opera浏览器 isOpera:function(){return navigator.userAgent.indexOf('Opera')>-1;}, // 判断是否为FireFox浏览器 isMoz:function(){return navigator.userAgent.indexOf('Mozilla/5.')>-1;} } //---------------------------------------------------------------------- // 查看程序运行时间 function Timing(){ this.startTime = 0; this.stopTime = 0; Timing.prototype.start = function(){ this.startTime = new Date(); }; Timing.prototype.stop = function(){ this.stopTime = new Date(); return (this.stopTime.getTime() - this.startTime.getTime()) + 'ms'; }; } //---------------------------------------------------------------------- //函数和对象绑定 function bindMethod(obj, f){ return function(){ return f.apply(obj, arguments); } } //对象 var obj = { color: 'red' }; //函数 function sayColor(str){ document.write(str + this.color); }; var fun = bindMethod(obj, sayColor); fun('The color is: '); // The color is: red //----------------------------------------------------------------------
/** * Map类 */ function Map() { this.entry = {}; this.mapSize = 0; }; Map.prototype = { put: function(key, value) { this.entry[key] = value; this.mapSize++; }, get: function(key) { if(!this.isEmpty()) { return this.entry[key]; } return null; }, remove: function(key) { if(!this.isEmpty()) { delete this.entry[key]; this.mapSize--; } return true; }, clear: function() { if(!this.isEmpty()) { for(var prop in this.entry) { delete this.entry[prop]; } this.mapSize = 0; } }, removeThis: function() { for(var prop in this) { delete this[prop]; } this.mapSize = null; }, containsKey: function(key) { if(typeof this.entry[key] !== "undefined") { return true; } return false; }, size: function() { return this.mapSize; }, isEmpty: function() { return this.mapSize == 0; }, getEntry: function() { return this.entry; }, toString: function() { var max = this.mapSize - 1; var buf = new StringBuffer(); var i = 0; buf.append("{"); for(var prop in this.entry) { buf.append(prop + "=" + this.entry[prop]); if(i < max) { buf.append(", "); } i++; } buf.append("}"); return buf.toString(); } };