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

Javascript es6将2个静态类合并到一个对象中

  •  1
  • bthe0  · 技术社区  · 8 年前

    我有以下代码:

    class A {
       static b() {
    
       }
    }
    
    class B {
      static c() {
    
      }
    }
    

    我试图将这两个静态类合并为一个:

    const combined = { ...A, ...B };
    

    然而,组合的对象会产生一个空的对象,而我希望对象包含所有组合的静态方法。

    我做错了什么?

    3 回复  |  直到 8 年前
        1
  •  1
  •   Matias Kinnunen    8 年前

    可以使用 Object.getOwnPropertyNames :

    class A {
      static staticMethod() {}
      nonStaticMethod() {}
    }
    
    console.log(Object.getOwnPropertyNames(A));

    有几个属性我们不感兴趣,即 prototype , length name . 我们可以手动过滤,例如:

    class A {
      static staticMethod() {}
      nonStaticMethod() {}
    }
    
    console.log(
      Object.getOwnPropertyNames(A)
        .filter(prop => prop !=='prototype' && prop !== 'length' && prop !== 'name')
    );

    好的现在我们可以创建 combined 对象并添加 A 的筛选方法:

    class A {
      static b() {
        console.log('called A.b');
      }
    }
    
    const classAMethods = Object.getOwnPropertyNames(A)
                            .filter(prop => prop !== 'prototype' &&
                                            prop !== 'length' &&
                                            prop !== 'name');
    
    const combined = {};
    combined.A = {};
    
    classAMethods.forEach(method => {
      combined.A[method] = () => A[method]();
    });
    
    console.log(combined);
    combined.A.b();

    如果你想打电话 combined.b() ,可以执行以下操作。请注意,这样,多个类中具有相同名称的方法将发生冲突。E、 g.如果两者都有 A.b B.b 定义 合并的 只能容纳其中一个。

    class A {
      static b() {
        console.log('called A.b');
      }
    }
    
    const classAMethods = Object.getOwnPropertyNames(A)
                            .filter(prop => prop !== 'prototype' &&
                                            prop !== 'length' &&
                                            prop !== 'name');
    
    const combined = {};
    
    classAMethods.forEach(method => {
      combined[method] = () => A[method]();
    });
    
    console.log(combined);
    combined.b();

    为了将所有这些结合在一起,我们有以下几点。注意,我使用 ...args 添加对调用类方法时传递参数的支持。

    class A {
      static b() {
        console.log('called A.b');
      }
    }
    
    class B {
      static c(name1, name2) {
        console.log('called B.c, hello', name1, 'and', name2);
        return 'returned by B.c';
      }
    }
    
    
    const getMethods = (cls) => Object.getOwnPropertyNames(cls)
                                  .filter(prop => prop !== 'prototype' &&
                                                  prop !== 'length' &&
                                                  prop !== 'name');
    
    const combined = {};
    
    const addMethodsToCombined = (cls) => {
      combined[cls.name] = {};
      getMethods(cls).forEach(method => {
        combined[cls.name][method] = (...args) => cls[method](...args);
      });
    };
    
    
    addMethodsToCombined(A);
    addMethodsToCombined(B);
    
    console.log(combined);
    combined.A.b();
    console.log(combined.B.c('world', 'universe'));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    向类添加新的静态方法时 A. B ,它们将自动通过 合并的 也如果创建新类 C ,你只需要打电话 addMethodsToCombined(C) .

        2
  •  1
  •   guest271314    8 年前

    可以在新对象上设置方法

    const combined = {A:A.b, B:B.c}
    
        3
  •  0
  •   synthet1c    8 年前

    您可以从A扩展B,然后从B扩展另一个类。

    如果您使用的是静态类,那么最好只使用对象文字

    class A {
       static b() {
          console.log('called A.b')
       }
    }
    
    class B extends A {
      static c() {
        console.log('called B.c')
      }
    }
    
    class Combined extends B {}
    
    Combined.b()
    Combined.c()