代码如下:
var YX = {
//得到JS内置数据类型的类型,返回值包括[Date,RegExp,Number,String,Array,Boolean,Function,null,undefined,Object]
getType : function(obj){
return obj == null ? obj + "" : Object.prototype.toString.call(obj).slice(8,-1);
}
//创建一个简单的类
,createClass : function(){
return function(){
this.init.apply(this,arguments);
};
}
//格式化字符串,YX.format("{0},{1},haha",["hello","world"])
,format : function(str,params){
var reg = /{(\d+)}/g;
return str.replace(reg,function(match,val){
return params[~~val];
});
}
//格式化字符串,YX.format2("数学={数学},语文={语文},haha",{"数学":100,"语文":99})
,format2 : function(str,params){
var reg = /{([^{}]+)}/g;
return str.replace(reg,function(match,val){
return params[val];
});
}
//格式化时间,YX.format3(new Date,"yy-mm-dd H:M:S")
,format3 : function(date,patten){
var y = date.getFullYear(),mon = date.getMonth() + 1,d = date.getDate(),h = date.getHours(),min = date.getMinutes(),s = date.getSeconds()
,zero = function(o){return ("0"+o).slice(-2)}
,matchs = {"yy":y,"y":(y+"").slice(-2),"mm":zero(mon),"m":mon,"dd":zero(d),"d":d,"HH":zero(h),"H":h,"MM":zero(min),"M":min,"SS":zero(s),"S":s};
return patten.replace(/yy|y|mm|m|dd|d|HH|H|MM|M|SS|S/g,function(match){
return matchs[match];
});
}
//数组去重,YX.unique([1,1,"1",document.body,document.body]
,unique : function(arr){
var kv = {},len = arr.length,rs = [],t;
for(;len--;){
t = arr[len];
(kv[t]==undefined || kv[t]!==t) && (kv[t]=t,rs.push(t));
}
return rs;
}
};
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!