Home  >  Article  >  Web Front-end  >  Advanced JavaScript (1) Extract public functions

Advanced JavaScript (1) Extract public functions

黄舟
黄舟Original
2017-02-11 14:32:281809browse

JS Extracting Public Functions

Problem

After experiencing a "large number" of project development, I found that more and more methods can be extracted and used as a public method. So, how to 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 2015-03-19 12:00:00 format. ShopStatementController


##

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


The converted rendering is as follows:

Method

For details, please see the blog post "Referencing another JS file in one JS file"

Add the following example code at the top of the calling file:


    document.write("<script language=javascript src=&#39;js/import.js&#39;></script>");


(Note: Sometimes the file you reference may also need to reference other js, we need to reference the required js file in the same way)

Implementation

Controller.js refers to Utils.js. You need to write the following statement at the top of controller.js:

document.write(

"4ef3f562aa0085bb48a437ba8fc7fa9b2cacc6d41bbb37262a98f745aa00fbf0");

Write the above method body in Utils.js.

The above is the content of JavaScript Advanced (1) Extracting Public Functions. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn