search

Home  >  Q&A  >  body text

javascript - Can class attributes and methods be separated into files in ES6?

To define class in es6, you can do this

class A {
    x = 1;
    y = 2;
    z = 3;
    mx () { // ... }
    my () { // ... }
    mz () { // ... }
}

When there are more and more A class attribute methods, it is found that the entire file has a large amount of code. Can some properties and methods be divided into a single file (for example, x and mx are divided into one x.js file, y and my are divided into one y.js file...)

If you use extends, you will find a problem. Properties cannot be shared, and neither can methods.

Can we combine (x.js / y.js / ...) into A when defining class A?

仅有的幸福仅有的幸福2704 days ago804

reply all(1)I'll reply

  • 阿神

    阿神2017-06-28 09:30:59

    MixinMode

    In short, it is to merge the prototype attributes of several classes, and the last big class can inherit this merged class.

    Ruan Yifeng’s ES6 tutorial has a sample program, you can refer to: Mixin-pattern implementation

    reply
    0
  • Cancelreply