Date Processing JS library (mini version)--self-built JS Library Summary _ Time and date

Source: Internet
Author: User
Tags inheritance time and date
interface + inheritance + code optimization idea
First share I think a very good JS programming tips, to achieve a large code sharing! Because a lot of JS Library will be in the original object on the direct prototype extension, but this is a bad habit, not only increased the memory consumption of each new instance object, but also easy to cause pollution misunderstanding (think there is this thing)! And this is the construction of JS Library a guideline: as little as possible to expand the prototype, especially the root of the object!

JS Building Code
JS Library criteria (Dean Edwards in the development of BASE2 some experience) translation: http://biaoge.me/2009/12/239 js build a good place to learn: http://ejohn.org/blog/ building-a-javascript-library/If you have time, then look at a building JS Library Super Front of the document, including CSS3, browser the latest APIs (such as Queryselector) Build-a-javascript-framework

improve code sharing with inheritance
Because does not extend on the native object, therefore needs an external namespace, but in this object will have some interfaces for the external call, but in order to improve the JS library's robustness, needs to minimize the external interface (the most ideal is only saves the user needs the interface)! So here's a question, I'll instantiate it:
Copy Code code as follows:

var namespace={
Iofirst:function () {},
Iosecond:function () {}
}

Under the object namespace have a thing to Iofirst and Iosecond share, but do not want to expose this interface! I am through the inheritance of all the external interface through a parent interface wrapper, and then in a Init method unified assignment, and in the Init method, due to the closure of the role of the code to achieve the common! Specific practices:

Dynamically adding external interfaces to enhance code flexibility
Copy Code code as follows:

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

}

To output the initialized interface on the console:
So all the external interfaces are bound to the pario below, so there are many interfaces can be less to knock a lot of code Oh! And the key to maintaining the internal and external hub of the Pario is responsible for locating the sub-interface, passing the parameters, and returning

Copy Code code as follows:

Pario:function () {
if (Date$.actionfun[arguments[0]])
{
var customfun=date$.actionfun[arguments[0]]
Return customfun.apply (Null,[arguments[1]]);
}
Else
Console&&cosnole.log ("Empty");
},

And you can see that there are three parts:
In the//set the common object and variable, we can write our common functions, variables, such as the decision browser, after adding a background-like Sqlhelp function is to initialize the interface:
Copy Code code as follows:

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);
}

And there is another advantage, is that your code will not give people a casual copy to use, because the interface of the internal contact is very large! For example, the above Funs.getweekbyday,funs.getnextweekbyday public Getweekbyday () Method! Finally attached to my immature JS library and interface statement, but also hope that Daniel help improve, thank you very much
Copy Code code as follows:

/*
Function:to compare two dates and return information ' equal '/' more '/' less '
Arguments Num:2
Arguments Type:date,date
Arguments declaration:1.the target object with need to compare (subtrahend); 2.the Compare Object (minuend)
Return data--type:string; Three value: ' More ' (if 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 to overload, so String or Date is allowed
Arguments declaration:if you String, it should the 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 to 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 to overload, so String or Date is allowed
Arguments Declaration:the day you want to get the ' the ' the ' the ' the ' the ' the ' in ' This weeek;if your 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 (the 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 {STARTDA Y,endday}
Arguments Num:1
Arguments Type:for this interface apply to overload, so String or Date is allowed
Arguments Declaration:the day you want to get the ' the ' the ' the ' the ' the ' the ' in ' This weeek;if your 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 (the 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 {STARTDA Y,endday}
Arguments Num:1
Arguments Type:date
Arguments Declaration:the day you are want to get the "the" the "
return Data--type:json; One value:{startday: "", Endday: ""}, you can get it by Var.startday (the Var is your custom variable)
Getmonthbyday:function (Day)
{
},
Function:to test if including today between the two dates you and return in Boolean
Arguments Num:2
Arguments Type:date,date
Arguments Declaration:the procedure'll test the larger and sort automatically, so the order is no matter
return data--Type:boolean; One value:true if is between the two dates
Todaybetween:function (Startdate,enddate)
{
},
Function:to caculate The difference between two dates in
Arguments Num:2
Arguments Type:date,date
Arguments Declaration:1.startdate (the one is 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 date of the year, have passed leap or not, in Boolean
Arguments Num:1
Arguments Type:for this interface apply to overload, so String or Date is allowed
Arguments declaration:if you String, it should the 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's leap year or false
Testleap:function (date)
{
} */

Welcome to Download: Date$.js

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.