JavaScript advanced (1) extract common functions

Source: Internet
Author: User
After going through a lot of project development, I found that more and more methods can be extracted and used as a public method. So how should we implement this idea in js? JS extraction from Public Functions

After going through a lot of project development, I found that more and more methods can be extracted and used as a public method. So how should we implement this idea in js?

Answer

For example, the following method is used to convert the standard time Thu Mar 19 2015 12:00:00 GMT + 0800 (China Standard Time) to the format 12:00:00. ShopStatementController


var formatDateTime = function (date) {      var y = date.getFullYear();      var m = date.getMonth() + 1;      m = m < 10 ? ('0' + m) : m;      var d = date.getDate();      d = d < 10 ? ('0' + d) : d;      var h = date.getHours();      var minute = date.getMinutes();      minute = minute < 10 ? ('0' + minute) : minute;  var second = date.getSeconds();    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second ; };


The converted values are as follows:

(Note: Sometimes the file you reference may need to reference other js files. We need to reference the desired js file in the same way)

Implementation

When controller. js references Utils. js, you must write the following statement at the top of controller. js:

Document. write ("

Related Article

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.