Small Program (6) Modular detailed introduction, small program modular
Modularization refers to extracting some common things into a file and exposing interfaces through module. exports. When we first created a project, there was an util. js file that was modularized for processing time.
/*** Process specific business logic */function formatTime (date) {// obtain the year, month, and day var year = date. getFullYear () var month = date. getMonth () + 1 var day = date. getDate () // obtain the time, minute, and second var hour = date. getHours () var minute = date. getMinutes () var second = date. getSeconds (); // format the date return [year, month, day]. map (formatNumber ). join ('/') + ''+ [hour, minute, second]. map (formatNumber ). join (':')} function formatNumber (n) {n = n. toString ( ) Return n [1]? N: '0' + n}/*** modular export exposure interface */module. exports = {formatTime: formatTime}
Usage:
// Import the modular method var util = require ('.. /.. /utils/util. js') Page ({data: {logs: []}, onLoad: function () {this. setData ({logs: (wx. getStorageSync ('logs') | []). map (function (log) {// call the modular method return util through the exposed interface. formatTime (new Date (log ))})})}})