微信小程序中全部 js 文件做用域皆为独立的,每个 js 文件即为一个模块。模块与模块之间的引用经过 module.exports 或 exports 对外暴露接口。node
注意:小程序
// common/tool.js =============================== function Hello(){ console.log("say hello!"); } function sayHi(){ console.log("Hi! I'm mirage. how are you"); } module.exports.Hello = Hello; exports.sayHi = sayHi; // index/index.js =============================== var tool = require("../common/tool.js"); Page({ onLoad:function(){ tool.Hello(); // 输出 say hello! tool.sayHi(); // 输出 Hi! I'm mirage. how are you })
引用模块也是 require(path) 官方注明:require 暂不支持绝对路径。微信小程序