I believe the correct thing to do in this case would be to assign the value of a as a property of the class. You could even pass it in on creation:
class A {
constructor(val = null) {
this.value = val;
}
this.max = function(b) {
if (this.value > b){
// do nothing
} else {
this.value = b;
}
}
}
Replace null with any default value you want, if you don't need to pass it in on creation, or just replace val in the constructor with the default value and take it out as an argument altogether:
class A {
constructor() {
this.value = aDefaultValueForAllInstances;
}
...
Or, with an arrow function, the method would be:
this.max = (b) => {
if (this.value > b){
// do nothing
} else {
this.value = b;
}
}
You could also simplify the if statement:
if (b < this.value){
this.value = b;
}
A good sign that you can simplify an if statement is if you aren't doing something in part of it.
If your code is actually this simple then, realistically, you're probably better off just leaving it procedural as it is.
a.max(b);, ifbis larger, then printingaon the next line will result in the value stored inb?A"