Home > Web Front-end > JS Tutorial > Let's talk about the method of abstracting public functions in JavaScript

Let's talk about the method of abstracting public functions in JavaScript

青灯夜游
Release: 2021-02-16 09:03:11
forward
3204 people have browsed it

Let's talk about the method of abstracting public functions in JavaScript

JS extract 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 implement the standard timeThu Mar 19 2015 12:00:00 GMT 0800 ( China Standard Time) is converted into the format of 2015-03-19 12:00:00. 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

See the blog post for details: http://blog.csdn.net/sunhuaqiang1/article/details/50450419

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 files 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.

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of Let's talk about the method of abstracting public functions in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
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