日期处理的js库(迷你版)--自建js库总结


Posted in Javascript onNovember 21, 2011

接口+继承+代码优化思想
先分享下我觉得一个很不错的js编程小技巧,达到很大的代码共用性! 因为很多js库会在原生的对象上进行直接原型扩展,但这是很不好的习惯,不仅加重了每个新实例对象的内存消耗,而且容易造成污染性误解(以为有这东西)!而这也是建js库一个准则:尽量少的原型扩展,特别是越根部的对象!

js建库准则
js建库准则(Dean Edwards在开发base2时候的一些体会)翻译版:http://biaoge.me/2009/12/239 js建库学习好地方:http://ejohn.org/blog/building-a-javascript-library/ 假如你有时间,再看一个建js库超级前沿的文档,包括css3,浏览器最新API(如querySelector) build-a-javascript-framework

用继承提高代码共用性
因为不在原生对象上进行扩展,所以需要一个对外的命名空间,而在这个对象下会有一些接口供外部调用,而为了提高本身js库的健壮性,需要在最大程度减小对外接口(最理想的就是只保存用户需要的接口)! 那么这里便有一个问题,我将它实例化吧:

var namespace={ 
IOfirst:function(){}, 
IOsecond:function(){} 
}

在对象namespace下有个东西需要给IOfirst跟IOsecond共用,而又不想暴露这个接口! 我是通过继承将所有对外接口通过一个父接口包装,然后在一个init方法下统一赋值,而在init这方法下,由于闭包的作用,达到了代码的共用性! 具体做法:

动态添加对外接口,加强代码灵活性

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 
}

在控制台上输出初始化完成的接口: 
日期处理的js库(迷你版)--自建js库总结于是所有对外接口都绑定到parIO下面,这样在有很多接口的情况下可以少敲好多代码哦! 而关键的维系内外部枢纽的parIO负责找到子接口,传参,并返回

parIO:function(){ 
if(Date$.actionFun[arguments[0]]) 
{ 
var customFun=Date$.actionFun[arguments[0]] 
return customFun.apply(null,[arguments[1]]); 
} 
else 
console&&cosnole.log("empty"); 
},

而可以看到有三部分: 
日期处理的js库(迷你版)--自建js库总结在 //set the common object and variable 那里我们可以写我们的公用函数,变量,如判断浏览器,加入类似后台的sqlHelp 函数 之后便是初始化接口了:
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); 
}

而且这样还有一个好处,就是你的代码不会给人家随便的复制去用,因为接口的内部联系性很大!例如上面的funs.getWeekByDay,funs.getNextWeekByDay公用了getWeekByDay()方法! 最后附上我的不成熟的js库以及接口声明,还望大牛帮忙改进下,万分感谢
/* 
//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
Javascript 相关文章推荐
JavaScript 定义function的三种方式小结
Oct 16 Javascript
JQuery Tab选项卡效果代码改进版
Apr 01 Javascript
JS实现的省份级联实例代码
Jun 24 Javascript
jQuery判断div随滚动条滚动到一定位置后停止
Apr 02 Javascript
JavaScript的null和undefined区别示例介绍
Sep 15 Javascript
String字符串截取的四种方式总结
Nov 28 Javascript
js实现图片360度旋转
Jan 22 Javascript
二维码图片生成器QRCode.js简单介绍
Aug 18 Javascript
jquery实现左右轮播图效果
Sep 28 jQuery
vue mint-ui tabbar变组件使用
May 04 Javascript
详解vue的双向绑定原理及实现
May 05 Javascript
vue如何限制只能输入正负数及小数
Jul 04 Javascript
js 完美图片新闻轮转效果,腾讯大粤网首页图片轮转改造而来
Nov 21 #Javascript
页面调用单个swf文件,嵌套出多个方法。
Nov 21 #Javascript
javascript中xml操作实现代码
Nov 21 #Javascript
js调用activeX获取u盘序列号的代码
Nov 21 #Javascript
15个款优秀的 jQuery 图片特效插件推荐
Nov 21 #Javascript
jQuery EasyUI API 中文文档 - TreeGrid 树表格使用介绍
Nov 21 #Javascript
jQuery EasyUI API 中文文档 - Tree树使用介绍
Nov 19 #Javascript
You might like
php 大数据量及海量数据处理算法总结
2011/05/07 PHP
smarty获得当前url的方法分享
2014/02/14 PHP
php实现网站留言板功能
2015/11/04 PHP
Eclipse PHPEclipse 配置的具体步骤
2017/08/08 PHP
基于jquery的lazy loader插件实现图片的延迟加载[简单使用]
2011/05/07 Javascript
IE6/7/8/9不支持exec的简写方式
2011/05/25 Javascript
JS对象与JSON格式数据相互转换
2012/02/20 Javascript
使用CSS和jQuery模拟select并附提交后取得数据的代码
2013/10/18 Javascript
jQuery在ul中显示某个li索引号的方法
2015/03/17 Javascript
javascript给span标签赋值的方法
2015/11/26 Javascript
JavaScript的兼容性与调试技巧
2016/11/22 Javascript
JS动态遍历json中所有键值对的方法(不知道属性名的情况)
2016/12/28 Javascript
Jquery EasyUI Datagrid右键菜单实现方法
2016/12/30 Javascript
webpack处理 css\less\sass 样式的方法
2017/08/21 Javascript
Vue 多层组件嵌套二种实现方式(测试实例)
2017/09/08 Javascript
详解webpack中的hash、chunkhash、contenthash区别
2018/01/05 Javascript
vue安装和使用scss及sass与scss的区别详解
2018/10/15 Javascript
[01:07:22]2014 DOTA2华西杯精英邀请赛 5 24 DK VS VG加赛
2014/05/26 DOTA
[53:18]Spirit vs Liquid Supermajor小组赛A组 BO3 第三场 6.2
2018/06/03 DOTA
[00:59]DOTA2背景故事第二期之四大基本法则
2020/07/07 DOTA
Python常用内置函数总结
2015/02/08 Python
Python实现快速排序算法及去重的快速排序的简单示例
2016/06/26 Python
Django框架 querySet功能解析
2019/09/04 Python
python实现删除列表中某个元素的3种方法
2020/01/15 Python
python和js交互调用的方法
2020/06/23 Python
Sofmap官网:日本著名的数码电器专卖店
2017/05/19 全球购物
绘儿乐产品官方在线商店:Crayola.com
2019/09/07 全球购物
2014年开学第一课活动方案
2014/03/06 职场文书
幼儿园小班教师寄语
2014/04/03 职场文书
日语专业毕业生自荐书
2014/06/18 职场文书
高中美术教师事迹材料
2014/08/22 职场文书
批评与自我批评范文
2014/10/15 职场文书
实施意见格式范本
2015/06/05 职场文书
2017元旦晚会开幕词
2016/03/03 职场文书
人生感悟经典句子
2019/08/20 职场文书
Python实现照片卡通化
2021/12/06 Python