Home  >  Article  >  Web Front-end  >  Let’s talk about the method of abstracting public functions in JavaScript

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

青灯夜游
青灯夜游forward
2021-02-16 09:03:113129browse

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

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

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

"4ef3f562aa0085bb48a437ba8fc7fa9b2cacc6d41bbb37262a98f745aa00fbf0");

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!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete