微信小程序 教程之模塊化
系列文章:
文件作用域
在JavaScript文件中聲明的變量和函數(shù)只在該文件中有效;不同的文件中可以聲明相同名字的變量和函數(shù),不會(huì)互相影響。
通過(guò)全局函數(shù)getApp()可以獲取全局的應(yīng)用實(shí)例,如果需要全局的數(shù)據(jù)可以在App()中設(shè)置,如:
// 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)
模塊化
我們可以將一些公共的代碼抽離成為一個(gè)單獨(dú)的js文件,作為一個(gè)模塊。模塊只有通過(guò)module.exports才能對(duì)外暴露接口。
// common.js function sayHello(name) { console.log('Hello ' + name + '!') } module.exports = { sayHello: sayHello }
在需要使用這些模塊的文件中,使用require(path)將公共代碼引入。
var common = require('common.js') Page({ helloMINA: function() { common.sayHello('MINA') } })
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
前端框架ECharts?dataset對(duì)數(shù)據(jù)可視化的高級(jí)管理
這篇文章主要為大家介紹了前端框架ECharts?dataset對(duì)數(shù)據(jù)可視化的高級(jí)管理,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12Intl對(duì)象DateTimeFormat?ListFormat?RelativeTimeFormat使用講解
這篇文章主要為大家介紹了Intl對(duì)象DateTimeFormat?ListFormat?RelativeTimeFormat使用講解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06LoadRunner調(diào)用JS加密后登錄實(shí)現(xiàn)
這篇文章主要為大家介紹了LoadRunner調(diào)用JS加密后登錄實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06微信小程序?qū)崿F(xiàn)給循環(huán)列表添加點(diǎn)擊樣式實(shí)例
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)給循環(huán)列表添加點(diǎn)擊樣式實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04