Home >Web Front-end >JS Tutorial >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 ? ('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 rendering is as follows:
##Method
See the blog post for details: http://blog.csdn.net/sunhuaqiang1/article/details/50450419Add the following example code at the top of the calling file:
document.write("<script language=javascript src='js/import.js'></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!