NodeJS ES6: Class module exports - node.js

Thank for read my topic, today, i have a quest about classes in es6, i don't why vi go error when i run.
I have class A:
var B = require("./b");
class A{
constructor() {
console.log("Constructor class A");
}
helloLoop() {
console.log("A: Hello loop");
B.hello();
}
helloWithoutLoop() {
console.log("A: Hello without loop");
}
}
module.exports = new A();
and class B:
var A = require("./a");
class B{
constructor() {
console.log("Constructor class B");
}
hello() {
console.log("B: Hello");
A.helloWithoutLoop();
}
}
module.exports = new B();
and a test:
var A = require("./a");
var B = require("./b");
A.helloLoop();
When i run test, i receive a error:
A.helloWithoutLoop();
TypeError: A.helloWithoutLoop is not a function
at B.hello ({work-sapce}\test\b.js:9:11)
I don't that happen!
Please help me!
Thanks very much!

You have a circular dependency. Your main file requires A which requires B which requires A (circular). Can't do that. The second attempt to require A before A itself has finished loading will return null which is why you get the weird error you do.
The usual solution is to find the common code and put it in a third module that both the others can load and not have A loading B and B loading A.
Have A loading C and B loading C.
Or, even simpler, just put A and B in the same file so there is no circular loading between them.

This is how you can call them :
import A from './A';
import B from './B';
A.helloLoop();
This is class A :
import B from './B';
class A{
constructor() {
console.log("Constructor class A");
}
helloLoop() {
console.log("A: Hello loop");
B.hello();
}
helloWithoutLoop() {
console.log("A: Hello without loop");
}
}
export default new A();
This is class B :
import A from './A';
class B{
constructor() {
console.log("Constructor class B");
}
hello() {
console.log("B: Hello");
A.helloWithoutLoop();
}
}
export default new B();
see the difference ?

Related

Get filename of derived class from base class in typescript running on node.js?

I'm looking for a way to get the filename of a derived class from a base class in typescript running on node.js. An example of this would be:
Foo.ts
export abstract class Foo {
constructor() { }
name() { return (__filename); }
print() { console.log(this.name()); }
}
Bar.ts
import { Foo } from './Foo';
export class Bar extends Foo {
constructor() { super(); }
}
main.ts
import { Bar } from './Bar';
let bar = new Bar();
bar.print(); // should yield the location of Bar.ts
Due to the number of files involved and just cleanliness I'd like this to be confined to the Foo class rather than having an override of the name() function in each derived class.
I was able to sort-of solve this with the code:
private getDerivedFilePath(): string {
let errorStack: string[] = new Error().stack.split('\n');
let ret: string = __filename;
let baseClass: any = ThreadPoolThreadBase;
for (let i: number = 3; i < errorStack.length; i++) {
let filename: string = errorStack[i].slice(
errorStack[i].lastIndexOf('(') + 1,
Math.max(errorStack[i].lastIndexOf('.js'), errorStack[i].lastIndexOf('.ts')) + 3
);
let other: any = require(filename);
if (other.__proto__ === baseClass) {
ret = filename;
baseClass = other;
} else {
break;
}
}
return (ret || '');
}
Added to Foo, which will work when called from the constructor to set a private _filename property, for inheritance chains beyond the example above so long as the files are structured with a default export of the class being used. There may also be a caveat that if a base class from which a derived object is inheriting directly is initialized as a separate instance within the constructor of any member of the inheritance chain it could get confused and jump to another independent derived class - so it's a bit of a hacky work-around and I'd be interested if someone comes up with something better, but wanted to post this in case someone stumbles across this question and it works for them.
You can use require.cache to get all cached NodeModule objects and filter it to find your module.
https://nodejs.org/api/modules.html#requirecache
class ClassA {
public static getFilePath():string{
const nodeModule = this.getNodeModule();
return (nodeModule) ? nodeModule.filename : "";
}
public static getNodeModule(): NodeModule | undefined{
const nodeModule = Object.values(require.cache)
.filter((chl) => chl?.children.includes(module))
.filter((mn)=> mn?.filename.includes(this.name))
.shift();
return nodeModule;
}
}
class ClassB extends ClassA {
constructor(){}
}
const pathA = ClassA.getFilePath(); //Must return the absolute path of ClassA
const pathB = ClassB.getFilePath(); //Must return the absolute path of ClassB

How to mock with JestJS injected dependency in class ES6?

I'm trying to get some dependance inversion going in a small NodeJS project. I want to have mocked instances of personal classes that I can inject in other classes.
This is for the lates node and jest version, I have read Jest documentation and nothing seem to be what I'm looking for.
class A {
getStr(): string {
return "bob";
}
}
class B {
private a: A;
constructor(a: A){
this.a = a;
}
getA(): string {
return a.getStr();
}
}
const b = new B(mocked instance of A)
I expect to be able to interact with the injected mock and see if it's been called in unit testing.
Assuming you would like to spy on the functions of A, you could do it as follows (if you would like to keep using classes notation):
class MockA {
constructor() {
this.getStr = jest.fn()
}
}
const mockedA = new MockA()
const b = new B(mockedA)
Then to test that it was called, you could do it as follows:
b.getA();
expect(a.getStr.mock.calls.length).toBe(1)
To create the mock without the class, you could do so as follows:
const mockedGetStr = jest.fn()
const b = new B({ getStr: mockedGetStr })
b.getA();
expect(a.getStr.mock.calls.length).toBe(1)

How to use class functions in another class?

How can i use class instance in another class like a pointer in C++ to class instance functions?
Example:
class A {
constructor()
{
this.block = [];
}
method()
{
return this.blocks.length;
}
}
another class:
class B {
constructor(instance)
{
this.instance = instance;
}
method()
{
this.instance.method(); // here i'm getting cannot get length of undefined
}
}
If i'm trying to to like that i'm getting problems to call it
You can try this. Here, when creating B class's instance I give into it an A class's instance as argument. Then inside B we can call A instance's methods, and access its properties.
Also, as #ViaTech posted you can use static methods to access them without needing to initialize an object of the class. That is what static methods is. Refer Static Methods
class B {
constructor(instance)
{
this.instance = instance;
}
method()
{
this.instance.method();
}
}
class A {
constructor()
{
}
method()
{
console.log("A's method");
}
}
var a = new A();
var b = new B(a);
b.method(); // A's method
You can easily do this in JS by calling a static method like so:
class A {
static write(){ //static method
console.log("Called write from A");
}
}
class B {
doIt(){
A.write();
}
}
let b = new B();
b.doIt();
Option 2, you instantiate the class in the constructor of the other like so:
class A {
write(){
console.log("Non-static write() called from class A");
}
}
class B {
constructor() {
this.a = new A();
}
doIt(){
this.a.write();
}
}
let b = new B();
b.doIt();
There are a few ways:
I accidentally switched between PHP and Javascript, but the principles are the same for both)
Use static functions:
Normally, you have a this in the class. Say you have this code:
class Car {
let color;
public function setColor(newColor){ this.color = newColor;}
}
let car = new Car();
car->setColor('green')`
The setColor function's this refers to that car. You can make let anotherCar = new Car(), then when you do anotherCar->setColor('red') you only change that car, not the first one. Simplistic: You can create multiple instances.
If you do not need that, but need the class once, you can make it static. A simple way to explain would be "you have a collection of seperate functions, just put into a wrapping class (which doesn't do a lot really)". For instance, you might have some sanatizing methods:
class Sanitize {
static function makeHtmlSave(input){
return doYourMagicHere(input);
}
static function removeXssCode(input){
return doMoreMagicHere(input);
}
}
This way, you can reuse it multiple times. If you want to use it, you do Sanitize::makeHtmlSave(someCode) where you need it. There isn't a Sanitize thing, it's just a wrapper to access the frunctions inside it.
Use extend:
You can extend a class. Say you have a generic class Vehicle, which has some properties (eg a motor, numberWeels, color) and you can extend that with more specific classes:
class Vehicle {
let color;
public function setColor(newColor){ this.color = newColor}
}
class Car extends Vehicle {
let hasAirco = false;
public function hasAirco(newValue){ this.hasAirco = newValue};
}
If you do let car = new Car(), you get a Car object, that extends/enlarges/complements the Vehicle class, so you can use both its (public) functions. Internally, Car can use the functions of Vehicle too.
Just pass it
class One {
// some stuff
}
class Two{
let otherObject;
construct(givenObject){
this.otherObject = givenObject;
}
}
You can now do this let a = new One(); let b = new Two(a);. You can not use the functions of One inside Two, but you can still use a->doSomething(). This solution feels like the easiest, but it almost never is. Classes/objects are tricky stuff, but I've rarely uses this solutions. There are use cases, but often it's a bad smell indicator.

Get path of required module in NodeJS

I have 2 classes, one that extends from the other. I need to have a method that can be called from extending classes but resolves the path to reflect whatever directory the extending class module is in.
/main.js
class Main {
getTemplate () {
return readFileSync(__dirname + './template.ejs')
}
}
/some/path/module.js (also contains /some/path/template.ejs)
class Module extends Main {
}
In main code
const m = new Module()
m.getTemplate() // /some/path/template.ejs
The above doesn't work since __dirname doesn't reflect the 'module.js' path.
I've played around with require.resolve but I am not sure how I can use this from within the module.
/main.js
class Main {
constructor(dirname) {
this.dirname = dirname;
}
getTemplate () {
return readFileSync(this.dirname + './template.ejs')
}
}
/some/path/module.js (also contains /some/path/template.ejs)
class Module extends Main {
constructor() {
super(__dirname);
}
}
Ok. I've got a working alas hokey solution.
class Main {
constructor () {
try {
throw new Error()
} catch(e) {
const m = e.stack.match(/at new \w+ \(([^:]+)/)
this.dirname = path.dirname(m[1])
}
}
getTemplate () {
return readFileSync(this.dirname + '/template.ejs')
}
}
class Module extends Main {
}

Check inheritance in nodejs

What is the best way to check inheritance in nodejs?
I'm trying to use instanceof in a instance of a class of another module that inherits a class for this module.
file a.js
class A{
}
class B extends A{
}
var b = new B();
b instanceof A ///this work
global.c instanceof A //this doesn't work
module.exports = A;
file c.js
var A = require("./a");
class C extends A{
}
global.c = new C();
It is because of loading issue! When you load class C, it request class A and it is run before the C is defined.
I have tried it myself, if I did it as you mentioned and requested both classes, the second one comparision failed.
However this one works:
a.js
class A{
callMeLaterAligator(){
console.log(b instanceof A) ///this work
console.log(global.c instanceof A) //this now work
}
}
class B extends A{
}
var b = new B();
module.exports = A;
c.js
var A = require("./a");
class C extends A{
}
global.c = new C();
The main method
require('services/c');
const a = require('services/a');
const aInst = new a();
aInst.callMeLaterAligator();
having output
true
true
To better understand whats going on, I have created this example
a.js
console.log('Hello, I am class A and I am not yet defined');
class A{
}
class B extends A{
}
var b = new B();
console.log('Hello, I am class A and I will compare something');
console.log(b instanceof A) ///this work
console.log(global.c instanceof A) //this doesn't work
module.exports = A;
c.js
console.log('Hello, I am class C and I am not yet defined');
var A = require("./a");
console.log('Hello, I am class C and I will now try to defined myself');
class C extends A{
}
console.log('Hello, I am class C and I am defined');
global.c = new C();
console.log('Hello, I am class C and I am in global.c');
server.js
require('services/c');
Having this output
Hello, I am class C and I am not yet defined
Hello, I am class A and I am not yet defined
Hello, I am class A and I will compare something
true
false
Hello, I am class C and I will now try to defined myself
Hello, I am class C and I am defined
Hello, I am class C and I am in global.c
If you change it to require "a" first, then the C is not loaded at all
server.js change :
require('services/a');
Having this output
Hello, I am class A and I am not yet defined
Hello, I am class A and I will compare something
true
false

Resources