search
HomeWeChat AppletMini Program DevelopmentFile scope analysis in WeChat applet

File scope analysis in WeChat applet

Sep 14, 2017 am 10:32 AM
ScopeAppletsdocument


File Scope

Variables and functions declared in a javaScript file are only valid in that file; they can have the same value in different files Variables and functions with the same names will not affect each other.
You can obtain the global application column through the global function getApp(). If you need global data, you can set it in app(), such as:

//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)

Modularization

You can extract some common code into a separate js file as a module. Modularization can only expose interfaces to the outside world through module.exports or exports.
It should be noted that:

  • wxports is a reference to module.exports, so you can change it at will in the modularization## Pointing to #exports will cause unknown errors. Therefore, it is recommended that developers use module.exports to expose module interfaces, unless you already clearly know the relationship between the two.

  • The mini program currently does not support the direct introduction of

    node_modules. When developers need to use node_modules, it is recommended to copy the relevant code to the mini program.

  • //commont.jsfunction sayHello(name){
        console.log('------  hello    ' + name +'=====');
    }
    module.exports.sayHello = sayHello;
    //index.jsvar common = require('../commont/commont.js');
    Page({    //加载视图的时候
      onLoad:function (){
       //调用
        common.sayHello('dqk');
    })
Console output in the directory:

File scope analysis in WeChat applet

Prompt

require Not available at the moment Supports absolute paths

The above is the detailed content of File scope analysis in WeChat applet. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.