復制代碼 代碼如下:
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;
}
};