欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序 教程之模塊化

 更新時(shí)間:2016年10月17日 16:27:48   投稿:lqh  
這篇文章主要介紹了微信小程序 模塊化的相關(guān)資料,需要的朋友可以參考下

系列文章:

微信小程序 教程之模塊化

微信小程序 教程之注冊(cè)頁(yè)面

微信小程序 教程之注冊(cè)程序

文件作用域

在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)文章

最新評(píng)論