Is it possible to import class methods in ES2015

I am creating a method in one module:

export function myMethod() {}

      

And creating an instance of a class in another module:

import {myMethod} from './methodFile';
class MyClass {
    constructor() {}
    myMethod // doesn't work
}

      

Can I be used myMethod

as part of a class MyClass

?

I am trying to create the equivalent of the following code:

class MyClass {
    constructor() {}
    myMethod() {}
}

      

+3


source to share


1 answer


No, you cannot refer to setpoints in ads class

.

However, syntax class

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



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

      

If your method has to use super

, you want to use the method.toMethod

.

+2


source







All Articles