js 实现私有变量的一种方法

临时变量存储数据   拦截实例函数 注入读取写入状态

如代码:

function objPrivate(){
    let isInPrivate = [false];
    const _cacheds=new Map()

    const getPrivateStatus=()=>{
      return isInPrivate[isInPrivate.length-1]
    }

    const makePrivate = function(fun){
      return function(...args){
        isInPrivate.push(true);
        let rs = fun.call(this,...args)
        if(rs instanceof Promise){
          rs.then((s)=>{
            isInPrivate.pop()
            return s;
          }).catch((e)=>{
            isInPrivate.pop()
            return Promise.reject(e)
          })
        } else {
          isInPrivate.pop()
        }

        return rs;
      }
    }

    const setPrivate = (key,val,defV) => {
      if(getPrivateStatus()){
        if(val ===undefined&&defV!==undefined){
          val = defV
        }
        _cacheds.set(key,val)
        return;
      }
      throw "can't write "+key
    }

    const getPrivate = function(...keys) {
      if(getPrivateStatus()){
        if(keys.length==1)
          return _cacheds.get(keys[0])
        return keys.map(k=>_cacheds.get(k))
      }
      throw "can't read "+keys.join(',')
    }

    const getIn = function(...keys){
      if(getPrivateStatus()){
        if(keys.length==1)
          return _cacheds.get(keys[0])
        let last = _cacheds.get(keys[0])
        for(let i=1;i<keys.length;i++){
          last = last[keys[i]]
        }
        return last
      }
      throw "can't read "+keys.join(',')
    }

    const setIn=function(keys,val,defV){
      if(getPrivateStatus()){
        if(val ===undefined&&defV!==undefined){
          val = defV
        }
        if(keys.length==1){
          _cacheds.set(keys[0],val)
          return
        }
          
        let last = _cacheds.get(keys[0])
        if(last ===undefined){
          last = {}
          _cacheds.set(keys[0],last)
        }
        
        for(let i=1,l=keys.length-1;i<l;i++){
            let now = last[keys[i]]
            if(now===undefined){
              last[keys[i]]=now={}
            } 
            last = now;
        }
        last[keys.length-1] = val;
      }
      throw "can't write "+key
    }

    const clearPrivate = ()=>{
      _cacheds.clear();
    }

    Object.keys(this).forEach(key=>{
      if(typeof this[key]==="function"){
        this[key] = makePrivate(this[key])
      }
    })



    this.$set = setPrivate
    this.$get = getPrivate
    this.$getIn = getIn
    this.$setIn = setIn
    this.$clear = clearPrivate
    this.$makeFunEnablePrivate = makePrivate
}

测试代码

 function A(){

      this.a1=function(){

            this.$set('a3',1)

      }

      this.a2=function(){

          console.log(this.$get('a3'))

      }

      objPrivate.call(this);
  }



  let a=new A();

  a.a1()

  a.a2()

  a.$set('a3',1)  // throw error

  a.$get('a3',4) // throw error

上一篇:pycharm无法识别已存在模块,如图,对于已存在的_init_paths.py和multitracker.py显示“unresolved reference xxx”:


下一篇:Exception in thread "main" java.lang.Error: Unresolved compilation problem: