Variables and functions declared in JavaScript files are only valid in this file. variables and functions with the same name can be declared in different files without affecting each other. File scope
Variables and functions declared in JavaScript files are only valid in this file. variables and functions with the same name can be declared in different files without affecting each other.
You can use the global function getApp () to obtain global application instances. if you need global data, you can set it in App (), for example:
// app.js App({ globalData: 1 })
// a.js // The localValue can only be used in file a.js. var localValue = 'a' // Get the app instance. var app = getApp() // Get the global data and change it. app.globalData++
// b.js // You can redefine localValue in file b.js, without interference with the localValue in a.js. var localValue = 'b' // If a.js it run before b.js, now the globalData shoule be 2. console.log(getApp().globalData)
Modular
We can extract some public code into a separate js file as a module. The interface can be exposed only through module. exports.
// common.js function sayHello(name) { console.log('Hello ' + name + '!') } module.exports = { sayHello: sayHello }
In files that need to use these modules, use require (path) to introduce public code.
var common = require('common.js') Page({ helloMINA: function() { common.sayHello('MINA') } })
The above is the modular content of the mini-program tutorial. For more information, see The PHP Chinese network (www.php.cn )!