常用的javascript函数
仿java类实现 /** * Map实现 */ var Map = function() { this.flag = false; this.store = new Array(); if (!this.flag) { Map.prototype.entry = function(key, val) { this.key = key; this.val = val; } Map.prototype.put = function(key, val) { this.store[this.store.length] = new this.entry(key, val); } Map.prototype.get = function(key) { for (var i = this.store.length - 1; i >= 0; i--) { if (this.store[i].key === key) return this.store[i].val; } return null; } Map.prototype.remove = function(key) { for (var i = this.store.length - 1; i >= 0; i--) { this.store[i].key === key && this.store.splice(i, 1); } } Map.prototype.keySet = function() { var keys = new Array(); for (var i = 0; i <= this.store.length - 1; i++) { keys.push(this.store[i].key); } return keys; } Map.prototype.valSet = function() { var vals = new Array(); for (var i = 0; i <= this.store.length - 1; i++) { vals.push(this.store[i].val); } return vals; } Map.prototype.clear = function() { this.store.length = 0; } Map.prototype.size = function() { return this.store.length; } this.flag = true; } } /** * StringBuilder实现 */ var StringBuilder = function() { this.vStrings = new Array(); this.flag = false; if (!this.flag) { StringBuilder.prototype.append = function(str) { this.vStrings.push(str); } StringBuilder.prototype.toString = function() { return this.vStrings.join(""); } StringBuilder.prototype.charAt = function(index) { return this.toString().charAt(index); } StringBuilder.prototype.clear = function() { this.vStrings.length = 0; } StringBuilder.prototype.Delete = function(start, end) { var tempString = this.toString(); var prevString = tempString.substring(0, start); var nextString = end ? tempString.substring(end) : tempString.substring(start + 1); this.clear(); this.append(prevString); this.append(nextString); } StringBuilder.prototype.length = function() { return this.toString().length; } StringBuilder.prototype.substring = function(start, end) { return this.toString().substring(start, end); } StringBuilder.prototype.replace = function(oldStr, newStr) { var newStr = newStr ? newStr : ""; var tempString = this.toString().replace(new RegExp(oldStr, "g"), newStr); this.clear(); this.append(tempString); } StringBuilder.prototype.indexOf = function(val) { return this.toString().indexOf(val); } StringBuilder.prototype.lastIndexOf = function(val) { return this.toString().lastIndexOf(val); } StringBuilder.prototype.insert = function(offset, str) { var prevString = this.substring(0, offset); var middleString = str; var nextString = this.substring(offset); this.clear(); this.append(prevString); this.append(middleString); this.append(nextString); } StringBuilder.prototype.split = function(sep) { return this.toString().split(sep); } this.flag = true; } } 为类库对象加入新方法 /** * @author wsf */ /** * 判断是否为数字型数据 */ if (!Object.prototype.isNumber) { String.prototype.isNumber = function() { return !this || isNuN(this.val()) ? false : (typeof this.val() === "number"); } } /** * 为数组添加remove方法 */ if (!Array.prototype.remove) { Array.prototype.remove = function(s) { for (var i = this.length; i >= 0; i--) { if (s === this[i]) this.splice(i, 1); } } } /** * 为String添加trim方法 */ if(!String.prototype.trim){ String.prototype.trim = function (){ this.replace(/\s*|\s$/g,""); } }
相关推荐
nmgxzm00 2020-11-10
ifconfig 2020-10-14
hhanbj 2020-11-17
zfszhangyuan 2020-11-16
古叶峰 2020-11-16
一个智障 2020-11-15
jipengx 2020-11-12
81427005 2020-11-11
xixixi 2020-11-11
游走的豚鼠君 2020-11-10
苗疆三刀的随手记 2020-11-10
Web卓不凡 2020-11-03
小飞侠V 2020-11-02
帕尼尼 2020-10-30
爱读书的旅行者 2020-10-26
帕尼尼 2020-10-23
杏仁技术站 2020-10-23
淼寒儿 2020-10-22