公共代码块

将一些公共的代码抽离成为一个单独的 js 文件,作为一个模块。模块只有通过 module.exports 或者 exports 才能对外暴露接口


/* 定义代码模块utils.js

const getVersion = () => {
    return "1.0.0"
}

module.exports = {
    getVersion: getVersion
}



/* 定义主程序方法app.js

App({

    name:"小程序",

    getName:function(){
        return this.name;
    }
})

/* 使用代码模块

// 引入公共JS模块
var util = require("../../utils/util.js");

// 获取主程序对象
var wx = getApp();

Page({
    onReady: function() {
        console.log(util.formatTime(new Date()));
        console.log(util.getVersion());
        console.log(wx.getName())
    }
})

你可能感兴趣的:(公共代码块)