使用vue数据辅助器,对于vue或angular等支持双向绑定的数据进行套壳,使ViewModel支持业务逻辑,从而是代码更合理

//一个用于自增的id
let $id = 0;


export default class  {

  bindings = {}

  helperKey = '$'

  helperCoreKey = 'core'

  helperIdKey = 'id'


/*
   增强方法
 
*/
  enh(core, Ht, configs) {

    let key = this.helperKey

    let coreKey = this.helperCoreKey

    let idKey = this.helperIdKey

    if (typeof Ht !== 'function') {
      throw new Error('参数2必须是函数')
    }

    if (typeof core !== 'object') {
      throw new Error('参数1必须是对象')
    }

    let $ = new Ht();

    if (configs) {
      //值复制操作
      for (let i in configs) {
        if (configs.hasOwnProperty(i)) {
          $[i] = configs[i];
        }
      }
    }

    let id ='id'+ ++$id;

    //id 是保留字段
    $[idKey] = id;

    $ = new Proxy($, {
      get(target, p) {
        let r = target[p]
        if (typeof r === 'function') { // 对函数做一层 处理防止其函数的this this指代被篡改
          return () => {
            r.call(target, arguments);
          };
        }
        return r;
      }
    })

    Reflect.defineProperty($, coreKey, {
      configurable: false,
      enumerable: false,
      writable: false,
      value: core
    })

    Reflect.defineProperty(core, key, {
      configurable: false,
      enumerable: false,
      writable: false,
      value: $
    })

    this.bindings[id]=$;

    //新建bindings对象,试
    this.bindings= Object.assign({}, this.bindings);
  }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值