Is it possible to import class methods in ES2015

后端 未结 1 1864
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-05 19:44

I\'m creating a method in one module:

export function myMethod() {}

And instantiating a class in another module:

import {my         


        
相关标签:
1条回答
  • 2021-01-05 20:24

    No, it is impossible to reference given values in class declarations.

    However, class syntax is mostly syntactic sugar, and prototype inheritance works as always. You can simply put the method on the prototype object after the class definition:

    import {myMethod} from './methodFile';
    class MyClass {
        …
    }
    MyClass.prototype.myMethod = myMethod;
    

    If your method needs to use super, you'll want to use the .toMethod method.

    0 讨论(0)
提交回复
热议问题