- 相關推薦
一些常用的JavaScript函數(shù)
復制代碼 代碼如下:
var YX = {
//得到JS內置數(shù)據(jù)類型的類型,返回值包括[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);
}
//創(chuàng)建一個簡單的類
,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("數(shù)學={數(shù)學},語文={語文},haha",{"數(shù)學":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];
});
}
//數(shù)組去重,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;
}
};
【一些常用的JavaScript函數(shù)】相關文章:
最常用的20個javascript方法函數(shù)09-10
淺析jQuery 遍歷函數(shù)javascript08-06
Javascript函數(shù)的定義和用法分析08-15
JavaScript中push(),join() 函數(shù)實例詳解09-05
常用的JavaScript模式09-22
JavaScript中常見的字符串操作函數(shù)及用法07-24
JavaScript常用方法匯總10-25