javascript - Is it possible to import class methods in ES2015 -


i'm creating method in 1 module:

export function mymethod() {} 

and instantiating class in module:

import {mymethod} './methodfile'; class myclass {     constructor() {}     mymethod // doesn't work } 

is possible use mymethod part of myclass class?

i'm trying create equivalent of following code:

class myclass {     constructor() {}     mymethod() {} } 

no, impossible reference given values in class declarations.

however, class syntax syntactic sugar, , prototype inheritance works always. can put method on prototype object after class definition:

import {mymethod} './methodfile'; class myclass {     … } myclass.prototype.mymethod = mymethod; 

if method needs use super, you'll want use .tomethod method.


Comments