# Modularization
# 1. required
Imports a module. Returns the interface exposed by the module via module.exports or exports.
Parameters
| Name | Type | Description |
|---|---|---|
| path | string | The relative path of the module file to be imported relative to the current file, or the npm module name, or the npm module path. Absolute paths are not supported. |
Example Code
// common.js
function sayHello(name) {
console.log(`Hello ${name} !`)
}
function sayGoodbye(name) {
console.log(`Goodbye ${name} !`)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
var common = require('common.js')
Page({
helloMINA: function() {
common.sayHello('MINA')
},
goodbyeMINA: function() {
common.sayGoodbye('MINA')
}
})
# 2. module
The current module object.
Properties
| Property | Type | Description |
|---|---|---|
| exports | Object | The object exposed by the module, which can be obtained when using require to reference this module. |
Example Code
// common.js
function sayHello(name) {
console.log(`Hello ${name} !`)
}
function sayGoodbye(name) {
console.log(`Goodbye ${name} !`)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye
# 3. exports
A reference to module.exports
Example Code
// common.js
function sayHello(name) {
console.log(`Hello ${name} !`)
}
function sayGoodbye(name) {
console.log(`Goodbye ${name} !`)
}
module.exports.sayHello = sayHello
exports.sayGoodbye = sayGoodbye