小程序之全局变量赋值

关于给全局变量赋值

onLoad:function(){
let that=this
if (storage != null && storage.length>0){
      for (var j = 0; j < storage.length; j++) {
        that.data.totalCount+=storage[j].orderDetail.number
      }

    }
}

错误,不可直接引用全局变量进行运算操作

正确方法:

引用变量,最后再赋值

onLoad:function(){
let that=this

let count=0

if (storage != null && storage.length>0){
      for (var j = 0; j < storage.length; j++) {
        count+=storage[j].orderDetail.number
      }
      
      that.setData({
          totalCount:count
      })

    }
}

你可能感兴趣的:(微信小程序,小程序,全局变量赋值)