代码之家  ›  专栏  ›  技术社区  ›  learningtech

无法获取javascript/reactjs中对象的方法

  •  0
  • learningtech  · 技术社区  · 7 年前

    我想我的问题可能是js的问题。我有两个班 Url QueryVars . 我想要 能够调用 QueryVars公司 ,好像我延伸了 QueryVars公司 . 所以我做了这样的事情:

    export default class Url {
    
      constructor()
      {
        this.queryVars = new QueryVars();
    
        console.log(this.queryVars);
        for(let x in this.queryVars)
        {
          console.log(x); // this never gets fired
          if(typeof this.queryVars[x] == "function")
          {
            this[x] = this.queryVars[x].bind(this.queryVars);
          }
        }
      }
    }
    
    default class QueryVars {
    
      init()
      {
        /* do something */
      }
      getQS(key)
      {
        /* do something */
      }
      getPage(key)
      {
        /* do something */
      }
    }
    

    问题是 console.log(x) 不会被解雇。我无法找回 this.queryVars

    Url.constructor 并绑定这些对象的更多方法,这样就好像我在实现多重继承,或者用其他类组成一个新类。这样我就可以:

    do url.getQS() instead of url.queryVars.getQS()
    do url.getIP() instead of url.ip.getIP()
    etc...
    

    我上面的代码是基于我在这个问题中所学到的: Is there a way to print all methods of an object in javascript?

    0 回复  |  直到 7 年前