日期處理的js庫(kù)(迷你版)--自建js庫(kù)總結(jié)
先分享下我覺得一個(gè)很不錯(cuò)的js編程小技巧,達(dá)到很大的代碼共用性! 因?yàn)楹芏鄇s庫(kù)會(huì)在原生的對(duì)象上進(jìn)行直接原型擴(kuò)展,但這是很不好的習(xí)慣,不僅加重了每個(gè)新實(shí)例對(duì)象的內(nèi)存消耗,而且容易造成污染性誤解(以為有這東西)!而這也是建js庫(kù)一個(gè)準(zhǔn)則:盡量少的原型擴(kuò)展,特別是越根部的對(duì)象!
js建庫(kù)準(zhǔn)則
js建庫(kù)準(zhǔn)則(Dean Edwards在開發(fā)base2時(shí)候的一些體會(huì))翻譯版:http://biaoge.me/2009/12/239 js建庫(kù)學(xué)習(xí)好地方:http://ejohn.org/blog/building-a-javascript-library/ 假如你有時(shí)間,再看一個(gè)建js庫(kù)超級(jí)前沿的文檔,包括css3,瀏覽器最新API(如querySelector) build-a-javascript-framework
用繼承提高代碼共用性
因?yàn)椴辉谠鷮?duì)象上進(jìn)行擴(kuò)展,所以需要一個(gè)對(duì)外的命名空間,而在這個(gè)對(duì)象下會(huì)有一些接口供外部調(diào)用,而為了提高本身js庫(kù)的健壯性,需要在最大程度減小對(duì)外接口(最理想的就是只保存用戶需要的接口)! 那么這里便有一個(gè)問(wèn)題,我將它實(shí)例化吧:
var namespace={
IOfirst:function(){},
IOsecond:function(){}
}
在對(duì)象namespace下有個(gè)東西需要給IOfirst跟IOsecond共用,而又不想暴露這個(gè)接口! 我是通過(guò)繼承將所有對(duì)外接口通過(guò)一個(gè)父接口包裝,然后在一個(gè)init方法下統(tǒng)一賦值,而在init這方法下,由于閉包的作用,達(dá)到了代碼的共用性! 具體做法:
動(dòng)態(tài)添加對(duì)外接口,加強(qiáng)代碼靈活性
addIO:function(str){
var arrs = str.split("."),
o = this;
for (i=(arrs[0] == "Date$") ? 1 : 0; i0)
{
var data=arrs[0]
o[arrs[i]]=o[arrs[i]] ||function(){return this.parIO.apply(null,[data,arguments])};
o=o[arrs[i]];
}
}
InitFuns:function(){
var that=this;
var funs=this.actionFun;
//init interface for all functions(test successfully)
var interfaces=["testLeap","disDayNum","todayBetween","getMonthByDay","getNextWeekByDay","getWeekByDay","newDate","compareDate"]
var shift;
do{
shift=interfaces.shift()
that.addIO(shift);
funs[shift]=function(){};
}while(interfaces.length>0)
//set the common object and variable
//for browers test
var br={
ie:false,//IE6~8
ff:false,//Firefox
ch:false//Chrome
}
var nav=navigator.userAgent;
if(!-[1,]) br.ie=true;
else if(nav.indexOf("Firefox")>0) br.ff=true;
else if(nav.indexOf("Chrome")>0) br.ch=true;
//continue to set IO
}
在控制臺(tái)上輸出初始化完成的接口: 于是所有對(duì)外接口都綁定到parIO下面,這樣在有很多接口的情況下可以少敲好多代碼哦! 而關(guān)鍵的維系內(nèi)外部樞紐的parIO負(fù)責(zé)找到子接口,傳參,并返回
parIO:function(){
if(Date$.actionFun[arguments[0]])
{
var customFun=Date$.actionFun[arguments[0]]
return customFun.apply(null,[arguments[1]]);
}
else
console&&cosnole.log("empty");
},
而可以看到有三部分:

funs.newDate=function(){
return formatDate(arguments[0][0])
}
funs.getWeekByDay=function(){
return getWeekByDay(arguments[0][0]);
}
funs.getNextWeekByDay=function(){
var speDate=formatDate(arguments[0][0]);
speDate.setDate(speDate.getDate()+7);
return getWeekByDay(speDate);
}
而且這樣還有一個(gè)好處,就是你的代碼不會(huì)給人家隨便的復(fù)制去用,因?yàn)榻涌诘膬?nèi)部聯(lián)系性很大!例如上面的funs.getWeekByDay,funs.getNextWeekByDay公用了getWeekByDay()方法! 最后附上我的不成熟的js庫(kù)以及接口聲明,還望大牛幫忙改進(jìn)下,萬(wàn)分感謝
/*
//function:to compare two dates and return information "equal"/"more"/"less"
//arguments num:2
//arguments type: Date,Date
//arguments declaration:1.the target object you need to compare(Subtrahend);2.the compare object(Minuend)
//return data -- type: String ; three value: "more"(if target is larger),"equal"(if target is equal to compared object),"less"(if target is smaller)
compareDate:function (objDate,comDate)
{
},
//function:to format the string to Date ,and return
//arguments num:1
//arguments type: for this interface apply for overload , so String or Date is allowed
//arguments declaration:if you pass String ,it should format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY:MM:DD" like "2008/10/12"
//return date : type:Date;one value:the date you want after formatting
newDate :function (str)
{
},
//function:get the start date and end date of the week of the date you have passed and return the Json including {startDay,endDay}
//arguments num:1
//arguments type:for this interface apply for overload , so String or Date is allowed
//arguments declaration:the day you want to get the first day and last day of in this weeek;if you pass String ,it should format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY:MM:DD" like "2008/10/12"
//return data--type :Json ; one value:{startDay:"",endDay:""} ,you can get it by var.startDay(var is your custom variable)
getWeekByDay :function (day)
{
},
//function:get the start date and end date of next week of the date you have passed and return the Json including {startDay,endDay}
//arguments num:1
//arguments type:for this interface apply for overload , so String or Date is allowed
//arguments declaration:the day you want to get the first day and last day of in this weeek;if you pass String ,it should format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY:MM:DD" like "2008/10/12"
//return data--type :Json ; one value:{startDay:"",endDay:""} ,you can get it by var.startDay(var is your custom variable)
getNextWeekByDay :function (day)
{
};
//function:get the start date and end date of the month of the date you have passed and return the Json including {startDay,endDay}
//arguments num:1
//arguments type:Date
//arguments declaration:the day you want to get the first day and last day of in this month
//return data--type :Json ; one value:{startDay:"",endDay:""} ,you can get it by var.startDay(var is your custom variable)
getMonthByDay :function (day)
{
},
//function:to test if including today between the two dates you pass and return in boolean
//arguments num:2
//arguments type:Date,Date
//arguments declaration:the procedure will test the larger and sort automatically ,so the order is no matter
//return data-- type: boolean ; one value :true if today is between the two dates
todayBetween :function (startDate,endDate)
{
},
//function:to caculate the difference between two dates in days
//arguments num:2
//arguments type:Date,Date
//arguments declaration:1.startDate(the one be reduced) 2.endDate(the one to reduce)
//return data--type:Number ; one value:the different between these two dates
disDayNum:function (startDate,endDate)
{
},
//function:test the year of the date you have passed leap or not and return in boolean
//arguments num:1
//arguments type: for this interface apply for overload , so String or Date is allowed
//arguments declaration:if you pass String ,it should format to "YYYY-MM-DD" or "YYYY/MM/DD" or "YYYY:MM:DD" like "2008/10/12"
//return data -- type:boolean ;one value: true if it is leap year or false
testLeap :function (date)
{
} */
歡迎下載:Date$.js
相關(guān)文章
JavaScript Timer實(shí)現(xiàn)代碼
最近開始接觸flex,比起javascript,感覺as3的Timer類甚是強(qiáng)大。而javascript只有裸體的setTimeout,setInternval。要實(shí)現(xiàn)稍微復(fù)雜一點(diǎn)的功能,稍微沒有底子的程序員就會(huì)把代碼寫的很亂。2010-02-02JavaScript 獲取當(dāng)前時(shí)間戳的代碼
JavaScript 獲取當(dāng)前時(shí)間戳的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2010-08-08日期處理的js庫(kù)(迷你版)--自建js庫(kù)總結(jié)
先推薦網(wǎng)上不錯(cuò)的日期js庫(kù):http://momentjs.com/ 其實(shí)這類資源網(wǎng)絡(luò)上一抓一把,但是想要針對(duì)項(xiàng)目實(shí)用的還是不多,因?yàn)槲医佑|的那類都經(jīng)常在日期而非深入到hour、minute、second!所以想干脆自己編個(gè)小庫(kù)吧,這樣以后寫代買將省力很多,在這里分享下,希望對(duì)大家有用2011-11-11用JavaScript將從數(shù)據(jù)庫(kù)中讀取出來(lái)的日期型格式化為想要的類型。
在頁(yè)面初始化時(shí),用JavaScript將從數(shù)據(jù)庫(kù)中讀取出來(lái)的日期型格式化為想要的類型。格式化為yyyy年MM月dd日 HH時(shí)mm分ss秒,大家可以參考下代碼,自由發(fā)揮下。2009-08-08javascript 日期聯(lián)動(dòng)選擇器 [其中的一些代碼值得學(xué)習(xí)]
javascript 日期聯(lián)動(dòng)選擇器,簡(jiǎn)單的看了下代碼非常不錯(cuò),都考慮到了標(biāo)準(zhǔn),兼容性也不錯(cuò)。學(xué)習(xí)js的朋友有福了。2009-12-12datePicker——日期選擇控件(with jquery)
用法很簡(jiǎn)單,而且js文件也很小,之前也見過(guò)一些日期選擇控件,但個(gè)頭都比較大,影響速度2007-02-02javascript 時(shí)間比較實(shí)現(xiàn)代碼
web開發(fā)中有時(shí)需要對(duì)輸入框中的時(shí)間(主要是開始時(shí)間和結(jié)束時(shí)間)進(jìn)行比較,網(wǎng)上搜索了一番,發(fā)現(xiàn)有不少是無(wú)效的,以下方法經(jīng)小弟檢驗(yàn)確實(shí)有效,特此共享。2009-10-10JS學(xué)習(xí)之一個(gè)簡(jiǎn)易的日歷控件
這種日歷控件實(shí)現(xiàn)起來(lái)不難,下面簡(jiǎn)單分析下我的思路2010-03-03