ES6如何覆盖构造函数的变量,以便子类继承覆盖值?

时间:2017-12-14 00:50:01

标签: javascript class ecmascript-6

目标:

  1. 创建对象/方法以在闭包/命名空间中运行以下内容:
  2. 从表单中收集数据。
  3. 与服务器交换数据。
  4. 将从服务器收到的数据和#1代码传递给#3成功后加载的新对象/方法。
  5. 我认为es6类可能是完美的,但遇到了一些问题,例如:

    class A {
      constructor(){
        this.myVar = 'foo';
      }
    
      dataXchange(data) {
        //send to server
        //set response:
        this.myVar = 'bar'; //serverData
        $(el).load(html); // this page includes script with class B
      }
    
      otherMethod () {}
    }
    //////////////////////
    class B extends A {
      constructor(myVar, otherMethod) {
        super(myVar, otherMethod);
        this.newVar = myVar;
      }
    
      // Do more stuff here or use otherMethod
    }
    

    明显的问题是instanceOfB.newVar === 'foo'而非bar

    我已经阅读了相当多的内容,似乎无法清楚地了解如何最好地实现目标。我慢慢自学2年,所以任何帮助都会受到赞赏。

0 个答案:

没有答案
相关问题