Home > Web Front-end > JS Tutorial > body text

Advanced JavaScript (1) Extract public functions

黄舟
Release: 2017-02-11 14:32:28
Original
1863 people have browsed it

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 ; 
};
Copy after login


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>");
Copy after login


(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(

"");

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)!


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template