I am unfamiliar with Javascript/Typescript environment so sorry if this is a basic question. I am trying to understand some code in our production.
Let's say I have some classes that are defined like so:
class Fruit {
errors: string[]
appleChecker = Apple(this);
bananaChecker = Banana(this);
// no constructor
doStuffOnApples() {
...
appleChecker.check();
return this.errors;
}
doStuffOnBananas() {
...
bananaChecker.check();
return this.errors;
}
}
class Apple {
fruitChecker: Fruit;
constructor(fruitChecker: Fruit) {
this.fruitChecker = fruitChecker;
}
check() {
...
this.fruitChecker.errors.push("This apple is dirty");
}
}
class Banana {
fruitChecker: Fruit;
constructor(fruitChecker: Fruit) {
this.fruitChecker = fruitChecker;
}
check() {
...
this.fruitChecker.errors.push("This banana is dirty");
}
}
The above is an oversimplification, but it represents the class structure I am trying to understand. I have 2 questions about the above:
1) In the lines
appleChecker = Apple(this);
bananaChecker = Banana(this);
does this
refer to the same instance of Fruit
, or 2 different instances?
2) Let's say I define a single instance of Fruit
, so fruitChecker = Fruit()
. Then I do the following in concurrent calls
fruitChecker.doStuffOnApples(apple1);
fruitChecker.doStuffOnApples(apple2);
For example, it could be concurrent because fruitChecker.doStuffOnApples
is called by an API with 2 different inputs concurrently. My question is, could the fruitChecker.errors
object be shared across both calls?