js-单体模式-用于设全局变量

实战使用场景,商城支付有港币,人民币,美元。根据用户的信息,设置币种的符号,在页面获取币种的符号。

let currencySymbol = {
    currency: '¥',
    get: function() {
      return this.currency
    },
    set: function(currency) {
      this.currency = currency
    }
  }
  console.log(currencySymbol.get())
  currencySymbol.set('%')
  console.log(currencySymbol)

  

在vue-cli中使用

// 单例模式,设置全局参数
Vue.prototype.$common = {
  value: 1,
  get: function () {
    return this.value
  },
  set: function (val) {
    this.bb = val
  }

  

posted @ 2019-06-29 12:43  fm060  阅读(229)  评论(0)    收藏  举报