小程序侦听全局参数方法

app.js中:  
// 侦听全局参数方法,val:侦听参数 fn:侦听参数变化后执行方法 不能深度侦听。
  myWatch:function(val,fn=()=>{}){
    Object.defineProperty(this.globalData,val,{
      configurable:true,
      enumerable:true,
      set:function(value){
        this['_'+val] = value
        fn(value)
      },
      get:function(){
        return this['_'+val]
      }
    })
  },
  globalData: {
    userInfo: null,
    session_key: null,
    open_id: null,
    latitude: 0,
    longitude: 0,
    hasOpenBluetooth: false,
    _hasHide:false // 初始参照值 无则undefined
  }
var thisApp = getApp();
其他页面onload方法中 
thisApp.myWatch(
'hasHide',(val)=>{ if(val){ // 关闭链接资源 console.log('close all') }else{ // 重启 console.log('need restart') } })
原文地址:https://www.cnblogs.com/wilsunson/p/11653984.html