if there is a way to split and make the code of every method in a separate file?
so I can have clean code and be simple and easier to maintain.
for now, I have a structure like this (minimal reproducible example):
// there is also a parentClass
// and I want to make sure that the method be inside parent
// so I can modify all the childs
// with the same method
class Parent {
// myMethod.js
myMethod() {
// my long code
}
//secondMethod.js
secondMethod() {
// my long code
}
// thirdMethod.js
thirdMethod() {
// my long code
}
}
class Child extends Parent {
constructor() {
this.someData = "someData";
}
}
// this need to work after putting the file externally for example
new Child.myMethod();
but I want that the methods be in separate files
the problem is how can a method know that is been part of a specific Class and not another Class for example.
for example If we want to separate the Child from parentClass, this is easy:
- thanks to
import
,export
,extends
import ParentClass from './ParentClass.js';
// here since we have "extends" the child class know that is been part of Parent.
export default class Child extends ParentClass {
// my child things and it will work
}
// but methods don't have extends or similar thing?
// and if yes how we can export them?
// export as a function?
// (but they aren't function, they are methods?)
so what do I want?
- method -> file
- file -> method can be exported
- method -> parentClass can import it
- import -> method be part of parentClass
- method ParentClass -> get extended to all childs
- method extended -> read (and change) correctly the values of
this.
(like it was before inside with code splitting)