Вызов методов класса внутри других включенных классов с Node.js

#node.js #class

Вопрос:

Для нового проекта в Node.js Мне нужно сократить файл кода на 15000 строк для клиента.

Я хочу использовать различные функции/методы в конкретных предметах в виде отдельных файлов .js.

Например: fruits.js, veggies.js, meat.js и т.д.

Я попробовал это с

 export { [function_names] };  

и так далее, но он не прочитал множество переменных и констант в основном файле, который настроен следующим образом:

 const APPLE = 'apple'; const PEAR = 'pear'; const MEAT = 'beef';   function fruitApple(){} function veggieBeans(){} function meatBeef(){}  

и так далее.

Когда я поместил функции в отдельные файлы и включил их в

 import { [function_names] } from fruits.js  

затем эти константы не работали и выдавали ошибки.

Это подводит меня к следующей попытке… использовать классы и конструкторы. Это «вроде» работает.

index.js

 const APPLE = 'apple'; const PEAR = 'pear'; const MEAT = 'beef'; // the included file   set used constants const Tester = require('./fruits'); const fruit = new Tester(APPLE, PEAR);  

в fruits.js

 class Tester {   // Construct the used constants in this class  constructor(APPLE, PEAR) {  this.somenewvalue = APPLE;  this.someoldvalue = PEAR;  }     testFruit(something) {  console.log('Start something else ... ', this.somenewvalue);  meat.testMeat(); **lt;lt;--- THIS NEEDS TO START A NEW CLASS IN MEAT.JS**  }  }  // Export the Class module.exports = Tester;  

My challenge lies in the calling of the second class method, because it is a chain of events. Each method in a class calls the next method/function. The last functions calls the First functions of the next class.

Like fruit methods gt; veggie methods gt; meat methods gt; done

How can I call the next method of a new class while also setting the constructor constants in that new file.

They are both to be included in the index.js, like:

 const Tester = require('./fruits'); const NextTester = require('./veggies');  const fruit = new Tester(APPLE, PEAR); const veggie = new NextTester(PEAR, MEAT);  // Initiate First Call fruit.testFruit('something');  

Надеюсь, я правильно понял, я никогда раньше не использовал Node, а с PHP это легко 😉