JavaScript类库D

因为是辅助类库,所以为了兼容所有其他框架和类库,采用了包装器的方式对对象进行扩展。D类库的最主要的内容是针对js常用内置对象的扩展,比如:String,Number,Array,Date等,这些扩展偏于具体的业务逻辑,比如对String扩展的trim方法、对Date扩展的toStr方法等,都是对一些常用但对象本身不支持且框架类库也不支持或不完整支持的功能扩展。同时通过对应包装器的包装我们可以通过链式方法来操作对象,最后每个包装器都提供了拆箱(即还原为原生对象)方法。故包装器提供的实质是一个装箱、操作、拆箱的过程。

命名空间:

复制代码 代码如下:


var D = {};


部分功能如下:

. String包装器

复制代码 代码如下:


(function(){
//包装String
D.str = function(s){
if(! (this instanceof y.str))return new y.str(s);
this.val = (s!==undefined) ? s.toString() : "";
};
D.str.prototype = {
//删除字符串两边空白
trim : function(type){
var types = {0:"(^\\s+)|(\\s+$)",1:"^\\s+",2:"\\s+$"};
type = type || 0;
this.val = this.val.replace(new RegExp(types[type],"g"),"");
return this;
},
//重复字符串
repeat : function(n){
this.val = Array(n+1).join(this.val);
return this;
},
//字符串两边补白
padding : function(len,dire,str){
if(this.val.length>=len)return this;
dire = dire || 0; //[0代表左边,1代表右边]
str = str || " "; //默认为一个空白字符
var adder = [];
for(var i=0,l = len - this.val.length; i<l;i++){
adder.push(str);
}
adder = adder.join("");
this.val = dire ? (this.val + adder) : (adder + this.val);
return this;
},
reverse : function(){
this.val = this.val.split("").reverse().join("");
return this;
},
byteLen : function(){
return this.val.replace(/[^\x00-\xff]/g,"--").length;
},
unBox : function(){
return this.val;
}
};
//alert(D.str(" 123 ").trim().repeat(2).padding(10,0,"x").reverse().unBox());
})();


.Array包装器

复制代码 代码如下:


(function(){
//包装Array
D.arr = function(arr){
if(!(this instanceof D.arr))return new D.arr(arr);
this.val = arr || [];
};
D.arr.prototype = {
each : function(fn){
for(var i=0,len=this.val.length;i<len;i++){
if(fn.call(this.val[i])===false){
return this;
}
}
return this;
},
map : function(fn){
var copy = [];
for(var i=0,len = this.val.length;i<len;i++){
copy.push(fn.call(this.val[i]));
}
this.val = copy;
return this;
},
filter : function(fn){
var copy = [];
for(var i=0,len=this.val.length;i<len;i++){
fn.call(this.val[i]) && copy.push(this.val[i]);
}
this.val = copy;
return this;
},
remove : function(obj,fn){
fn = fn || function(m,n){
return m===n;
};
for(var i=0,len = this.val.length;i<len;i++){
if(fn.call(this.val[i],obj)===true){
this.val.splice(i,1);
}
}
return this;
},
unique : function(){
var o = {},arr = [];
for(var i=0,len = this.val.length;i<len;i++){
var itm = this.val[i];
(!o[itm] || (o[itm]!==itm) )&& (arr.push(itm),o[itm] = itm);
}
this.val = arr;
return this;
},
indexOf : function(obj,start){
var len = this.val.length,start = ~~start;
start < 0 && (start+= len);
for(;start<len;start++){
if(this.val[start]===obj)return start;
}
return -1;
},
lastIndexOf : function(obj,start){
var len = this.val.length,start = arguments.length === 2 ? ~~start : len-1;
start = start < 0 ? (start+len) : (start>=len?(len-1):start);
for(;start>-1;start--){
if(this.val[start] === obj)return start;
}
return -1;
},
unBox : function(){
return this.val;
}
};
//alert( D.arr(["123",123]).unique().unBox());
//alert(D.arr([1,2,3]).map(function(i){return ++i;}).filter(function(i){return i>2;}).remove(3).unBox());
})();


.Number包装器

复制代码 代码如下:


(function(){
//包装Number
D.num = function(num){
if(!(this instanceof D.num))return new D.num(num);
this.val = Number(num) || 0;
};
D.num.prototype = {
padZore : function(len){
var val = this.val.toString();
if(val.length>=len)return this;
for(var i=0,l = len-val.length;i<l;i++){
val = "0" + val;
}
return val;
},
floatRound : function(n){
n = n || 0;
var temp = Math.pow(10,n);
this.val = Math.round(this.val * temp)/temp;
return this;
},
unBox : function(){
return this.val;
}
};
//alert(D.num(3.1235888).floatRound(3).unBox());
})();


.Date包装器

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdxwjp.html