日期处理的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 Base64编码和解码,实现URL参数传递。
Sep 18 Javascript
用jQuery简化JavaScript开发分析
Feb 19 Javascript
面向对象的Javascript之二(接口实现介绍)
Jan 27 Javascript
js使用for循环与innerHTML获取选中tr下td值
Sep 26 Javascript
Angularjs注入拦截器实现Loading效果
Dec 28 Javascript
AngularJS实践之使用NgModelController进行数据绑定
Oct 08 Javascript
jquery组件WebUploader文件上传用法详解
Oct 23 Javascript
jQuery实现的简单排序功能示例【冒泡排序】
Jan 13 Javascript
基于axios封装fetch方法及调用实例
Feb 05 Javascript
Vue中使用vue-i18插件实现多语言切换功能
Apr 25 Javascript
vue 监听键盘回车事件详解 @keyup.enter || @keyup.enter.native
Aug 25 Javascript
微信 jssdk 签名错误invalid signature的解决方法
Jan 14 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
基于MySQL体系结构的分析
2013/05/02 PHP
php调用新浪短链接API的方法
2014/11/08 PHP
PHP将进程作为守护进程的方法
2015/03/19 PHP
php结合redis高并发下发帖、发微博的实现方法
2016/12/15 PHP
JavaScript传递变量: 值传递?引用传递?
2011/02/22 Javascript
JQueryEasyUI datagrid框架的进阶使用
2013/04/08 Javascript
jQuery 2.0.3 源码分析之core(一)整体架构
2014/05/27 Javascript
js实现点击左右按钮轮播图片效果实例
2015/01/29 Javascript
jQuery实现模拟marquee标签效果
2015/07/14 Javascript
在AngularJS框架中处理数据建模的方式解析
2016/03/05 Javascript
jQuery过滤特殊字符及JS字符串转为数字
2016/05/26 Javascript
JavaScript跨域调用基于JSON的RESTful API
2016/07/09 Javascript
vue.js学习笔记之绑定style样式和class列表
2016/10/31 Javascript
Vue获取DOM元素样式和样式更改示例
2017/03/07 Javascript
vue-cli开发环境实现跨域请求的方法
2018/04/07 Javascript
JS实现点击按钮可实现编辑功能
2018/07/03 Javascript
jQuery的ztree仿windows文件新建和拖拽功能的实现代码
2018/12/05 jQuery
Javascript操作select控件代码实例
2020/02/14 Javascript
js利用iframe实现选项卡效果
2020/08/09 Javascript
Python使用剪切板的方法
2017/06/06 Python
python学习教程之Numpy和Pandas的使用
2017/09/11 Python
python调用API实现智能回复机器人
2018/04/10 Python
Python 字符串与二进制串的相互转换示例
2018/07/23 Python
如何使用Python标准库进行性能测试
2019/06/25 Python
通过实例了解python property属性
2019/11/01 Python
完美解决keras 读取多个hdf5文件进行训练的问题
2020/07/01 Python
Python3 + Appium + 安卓模拟器实现APP自动化测试并生成测试报告
2021/01/27 Python
深入理解HTML5定时器requestAnimationFrame的使用
2018/12/12 HTML / CSS
芭比波朗加拿大官方网站:Bobbi Brown Cosmetics CA
2020/11/05 全球购物
新闻专业应届生求职信
2013/10/31 职场文书
大学生收银员求职信分享
2014/01/02 职场文书
干部考核评语
2014/04/29 职场文书
娱乐节目策划方案
2014/06/10 职场文书
创建绿色学校先进个人材料
2014/08/20 职场文书
2014年社区卫生工作总结
2014/12/18 职场文书
2016三八妇女节慰问信
2015/11/30 职场文书