Passing an object of a class using 'this' to another class in Javascript
So, I come from a background of java, c#, python and other languages of that range, and I'm trying to basically recreate something in Javascript.
I want to pass an object of Class A to Class B using 'this' so an instance of Class A can be accessed in Class B.
import B from .....
class A
{
constructor()
{
Obj = new B // Make object of class B
b(this) // Send instance of class A to class B
}
methodA() // Method to test
{
alert("Hello!")
}
}
class B
{
constructor(a) //receive class A here in class B
{
a.methodA // call method of the instance class A
}
}
I cannot access methodA in b when passing A into B
extendsfor this: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…thisand inheritance in javascript. It is not as in Java and IMHO you should not try to shoe horn java-this and java-inheritance into javascript. Instead embrace how Javscript is intended to work. I have no good links for you but the book "Javascript, the good parts" is a good investment in time to read. It tells you where javascript is wrong, quirky and good. Especially the latter is valueable to know, what to embrace. FWIWthisinside a class method, since it's the object that the method was called on. What do you expectb(this)to do outside of a method?