Modularization of mini-program tutorials and mini-program modularization
Series of articles:
Modularization of mini-program tutorials
Registration page for applet tutorials
Registration Procedures for mini-program tutorials
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.jsApp({ 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.jsfunction 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') }})
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!