国产99久久精品_欧美日本韩国一区二区_激情小说综合网_欧美一级二级视频_午夜av电影_日本久久精品视频

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期

來源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 21:01:25
文檔

日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期

日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期:接口+繼承+代碼優(yōu)化思想 先分享下我覺得一個(gè)很不錯(cuò)的js編程小技巧,達(dá)到很大的代碼共用性! 因?yàn)楹芏鄇s庫會在原生的對象上進(jìn)行直接原型擴(kuò)展,但這是很不好的習(xí)慣,不僅加重了每個(gè)新實(shí)例對象的內(nèi)存消耗,而且容易造成污染性誤解(以為有這東西)!而這也是建j
推薦度:
導(dǎo)讀日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期:接口+繼承+代碼優(yōu)化思想 先分享下我覺得一個(gè)很不錯(cuò)的js編程小技巧,達(dá)到很大的代碼共用性! 因?yàn)楹芏鄇s庫會在原生的對象上進(jìn)行直接原型擴(kuò)展,但這是很不好的習(xí)慣,不僅加重了每個(gè)新實(shí)例對象的內(nèi)存消耗,而且容易造成污染性誤解(以為有這東西)!而這也是建j

接口+繼承+代碼優(yōu)化思想
先分享下我覺得一個(gè)很不錯(cuò)的js編程小技巧,達(dá)到很大的代碼共用性! 因?yàn)楹芏鄇s庫會在原生的對象上進(jìn)行直接原型擴(kuò)展,但這是很不好的習(xí)慣,不僅加重了每個(gè)新實(shí)例對象的內(nèi)存消耗,而且容易造成污染性誤解(以為有這東西)!而這也是建js庫一個(gè)準(zhǔn)則:盡量少的原型擴(kuò)展,特別是越根部的對象!

js建庫準(zhǔn)則
js建庫準(zhǔn)則(Dean Edwards在開發(fā)base2時(shí)候的一些體會)翻譯版:http://biaoge.me/2009/12/239 js建庫學(xué)習(xí)好地方:http://ejohn.org/blog/building-a-javascript-library/ 假如你有時(shí)間,再看一個(gè)建js庫超級前沿的文檔,包括css3,瀏覽器最新API(如querySelector) build-a-javascript-framework

用繼承提高代碼共用性
因?yàn)椴辉谠鷮ο笊线M(jìn)行擴(kuò)展,所以需要一個(gè)對外的命名空間,而在這個(gè)對象下會有一些接口供外部調(diào)用,而為了提高本身js庫的健壯性,需要在最大程度減小對外接口(最理想的就是只保存用戶需要的接口)! 那么這里便有一個(gè)問題,我將它實(shí)例化吧:
代碼如下:
var namespace={
IOfirst:function(){},
IOsecond:function(){}
}

在對象namespace下有個(gè)東西需要給IOfirst跟IOsecond共用,而又不想暴露這個(gè)接口! 我是通過繼承將所有對外接口通過一個(gè)父接口包裝,然后在一個(gè)init方法下統(tǒng)一賦值,而在init這方法下,由于閉包的作用,達(dá)到了代碼的共用性! 具體做法:

動態(tài)添加對外接口,加強(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

}

在控制臺上輸出初始化完成的接口:
初始化接口完成于是所有對外接口都綁定到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");
},

而可以看到有三部分:
在 //set the common object and variable 那里我們可以寫我們的公用函數(shù),變量,如判斷瀏覽器,加入類似后臺的sqlHelp 函數(shù) 之后便是初始化接口了:
代碼如下:
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è)好處,就是你的代碼不會給人家隨便的復(fù)制去用,因?yàn)榻涌诘膬?nèi)部聯(lián)系性很大!例如上面的funs.getWeekByDay,funs.getNextWeekByDay公用了getWeekByDay()方法! 最后附上我的不成熟的js庫以及接口聲明,還望大牛幫忙改進(jì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

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期

日期處理的js庫(迷你版)--自建js庫總結(jié)_時(shí)間日期:接口+繼承+代碼優(yōu)化思想 先分享下我覺得一個(gè)很不錯(cuò)的js編程小技巧,達(dá)到很大的代碼共用性! 因?yàn)楹芏鄇s庫會在原生的對象上進(jìn)行直接原型擴(kuò)展,但這是很不好的習(xí)慣,不僅加重了每個(gè)新實(shí)例對象的內(nèi)存消耗,而且容易造成污染性誤解(以為有這東西)!而這也是建j
推薦度:
標(biāo)簽: 時(shí)間日期 日期 js
  • 熱門焦點(diǎn)

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 91精品国产91久久久久久最新 | 日韩精品在线观看免费 | 欧美在线日韩 | 精品一区二区三区亚洲 | 日本久久精品免视看国产成人 | 日本一区二区三区在线观看 | 亚洲欧美另类专区 | 久久精品国产欧美成人 | 国产一区二区在线播放 | 天天曰夜夜操 | 日韩精品亚洲电影天堂 | 国产一区二区不卡 | 中文亚洲欧美日韩无线码 | 亚洲欧美天堂 | 欧美日韩国产综合视频在线看 | 国产亚洲欧美另类一区二区三区 | 韩日欧美 | 成人爽a毛片在线视频 | 91视频国产91久久久 | 亚洲高清在线视频 | 欧美精品亚洲精品 | 日本国产最新一区二区三区 | 国产在线中文字幕 | 久操视频免费在线观看 | 精品视频免费看 | 精品国产免费一区二区三区五区 | 欧美a色爱欧美综合v | 亚洲成人综合在线 | 国内精品91久久久久 | 日韩欧美在线综合网高清 | 欧日韩视频| 黄a免费 | 国产视频一 | 日韩欧美高清一区 | 日韩免费视频在线观看 | 亚洲视频二 | 国产片欧美片亚洲片久久综合 | 久久久久久久岛国免费播放 | 久久国产精品一区二区三区 | 另类一区 | 国产精品一区二区三区四区五区 |