- 相關(guān)推薦
一些常用的JavaScript函數(shù)
復(fù)制代碼 代碼如下:
var YX = {
//得到JS內(nèi)置數(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)建一個(gè)簡(jiǎn)單的類
,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ù)學(xué)={數(shù)學(xué)},語(yǔ)文={語(yǔ)文},haha",{"數(shù)學(xué)":100,"語(yǔ)文":99})
,format2 : function(str,params){
var reg = /{([^{}]+)}/g;
return str.replace(reg,function(match,val){
return params[val];
});
}
//格式化時(shí)間,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ù)】相關(guān)文章:
最常用的20個(gè)javascript方法函數(shù)09-10
淺析jQuery 遍歷函數(shù)javascript08-06
Javascript函數(shù)的定義和用法分析08-15
JavaScript中push(),join() 函數(shù)實(shí)例詳解09-05
JavaScript日期時(shí)間格式化函數(shù)08-29
常用的JavaScript模式09-22
JavaScript中常見的字符串操作函數(shù)及用法07-24
JavaScript常用方法匯總10-25