tencent cloud

All product documents
Tencent Cloud Super App as a Service
Modularization
Last updated: 2025-02-24 17:10:38
Modularization
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"


Was this page helpful?
You can also Contact Sales or Submit a Ticket for help.
Yes
No

Feedback

Contact Us

Contact our sales team or business advisors to help your business.

Technical Support

Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

7x24 Phone Support
Hong Kong, China
+852 800 906 020 (Toll Free)
United States
+1 844 606 0804 (Toll Free)
United Kingdom
+44 808 196 4551 (Toll Free)
Canada
+1 888 605 7930 (Toll Free)
Australia
+61 1300 986 386 (Toll Free)
EdgeOne hotline
+852 300 80699
More local hotlines coming soon