# Modularity
You can extract some common code into a separate js file as a module. Modules can only expose their interfaces externally through module.exports
or exports
.
Notice:
-exports
is a reference to module.exports
, so changing the direction of exports
in the module at will will cause unknown errors. Therefore, it is recommended that developers use module.exports
to expose module interfaces, unless you have a clear understanding of the relationship between the two.
-Mini programs currently do not support the direct import of node_modules
. When developers need to use node_modules
, it is recommended to copy the relevant code to the directory of the mini program, or use the [npm](https://developers. weixin.qq.com/miniprogram/dev/devtools/npm.html) function.
// common.js
function sayHello(name) {
console.log(`Hello little game framework!`)
}
function sayGoodbye(name) {
console.log(`Goodbye game framework!`)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
# File scope
Variables and functions declared in a JavaScript file are only valid in that file; variables and functions with the same name can be declared in different files without affecting each other.
# Global Object
Similar to the browser's Window
and NodeJS's global
, the mini game also has a global object GameGlobal
. Variables can be passed in multiple files through GameGlobal
.
// a.js
GameGlobal.globalData = 1
// b.js
console.log(GameGlobal.globalData) // output "1"