An example Analysis of scope and Modularization in WeChat Mini Programs
This article mainly shows you the "sample Analysis of scope and Modularization in WeChat Mini Programs", which is easy to understand and well-organized. I hope it can help you solve your doubts. Next, let the editor lead you to study and learn the article "sample Analysis of scope and Modularization in WeChat Mini Programs".
File scope
Variables and functions declared in the JavaScript file are valid only in that file; variables and functions with the same name can be declared in different files without affecting each other.
Example:
The global application instance can be obtained through the global function getApp (). If you need global data, you can set it in App (), such as:
/ * app.js * / App ({globalData: 1}) / * a.js * / / this is the local variable localValue var localValue ='a' / / get the instance of app.js var app = getApp () / / manipulate the global variable app.globalData++/* b.js through the example of app * / / the variable localValue can be defined repeatedly in different files var localValue ='b' / / if the a.js file is executed first Then what b.js gets is the variable value console.log (getApp (). GlobalData) executed by a.js.
Modularization
We can extract some common code into a separate js file as a module. The module can expose the interface only through module.exports or exports.
Example:
/ * 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 common code
/ * a.js * / var common = require ('common.js') Page ({helloMINA: function () {common.sayHello (' MINA')}}) these are all the contents of this article entitled "sample Analysis of scope and Modularization in WeChat Mini Programs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!