tencent cloud

Tencent Cloud Super App as a Service

Modularization

PDF
Focus Mode
Font Size
Last updated: 2025-02-24 17:10:38
Some common code can be extracted into a separate JS file as a module. A module can only expose its API through module.exports or exports .
Note:
exports is a reference to module.exports, so changing the reference of exports within the module can cause unexpected errors. Therefore, it is recommended to use module.exports to expose module APIs unless you clearly understand the relationship between the two.
// common.js
function sayHello(name) {
console.log(Hello ${name} !)
}
function sayGoodbye(name) {
console.log(Goodbye ${name} !)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye

File scope

Variables and functions declared in a JavaScript file are only effective within that file. Different files can declare variables and functions with the same name without affecting each other.

Global object

Similar to the window object in browsers and the global object in NodeJS , mini games have a global object called GameGlobal. You can use GameGlobal to pass variables between multiple files.
// a.js
GameGlobal.globalData = 1
// b.js
console.log(GameGlobal.globalData) // Output "1"



Help and Support

Was this page helpful?

Help us improve! Rate your documentation experience in 5 mins.

Feedback