JS开发自己的类库实例分析
本文实例分析了JS开发自己的类库。分享给大家供大家参考,具体如下:
上文说过,复用性主要有:插件,插件为功能的基本单元;组件,组件为应用的单元;设计模式,设计模式为解决为题的思路。
上述三者是算法的具体表现形式。
基于上面的认识,减少重复造轮子的时间,实现高效开发,决定开发自己的类库。
下面是第一个函数,时间戳转为具体时间;
//时间戳转为具体时间
functiontransform(now){
vard=newDate(now);
varyear=d.getFullYear();
varmonth=d.getMonth()+1;
varday=d.getDate();
varhour=d.getHours();
varminute=d.getMinutes();
varsecond=d.getSeconds();
if(month<10){
month="0"+month;
}
vardate=year+"-"+month+"-"+day+
""+hour+":"+minute+":"+second;
returndate;
}
反过来具体时间转为时间戳:
//具体时间转转为指定时间戳
functiontransdate(date){
vard=newDate();
d.setFullYear(date.substring(0,4));
d.setMonth(parseInt(date.substring(5,7))-1);
d.setDate(date.substring(8,10));
d.setHours(date.substring(11,13));
d.setMinutes(date.substring(14,16));
d.setSeconds(date.substring(17,19));
returnDate.parse(d)/1000;
}
也可以写成棉城对象的形式:
vartransform=function(){
};
transform.prototype.change=function(t){
vard=newDate(t);
this.year=d.getFullYear();
this.month=d.getMonth()+1;
this.day=d.getDate();
this.hour=d.getHours();
this.minute=d.getMinutes();
this.second=d.getSeconds();
if(this.month<10){
this.month="0"+this.month;
}
this.date=this.year+"-"+this.month+"-"+this.day+
""+this.hour+":"+this.minute+":"+this.second;
returnthis.date;
}
好了,关于转换的主函数就是这个了,实际应用当中具体时间转为时间戳,会根据具体情况进行变化处理。
更多关于JavaScript相关内容可查看本站专题:《JavaScript页面元素操作技巧总结》、《JavaScript操作DOM技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。