I was getting max stack size errors with the original version of my class (without underscores), and when I looked it up, I ended up seeing some examples that included adding an underscore to the class property when referencing it in the getter/setter; before getting too fancy with anything else that was recommended, I just added the underscores and ran the program, and this alone fixed the problem. How does this work? What is the underscore doing in this context? From what I understand, it is preventing the stack from being overloaded from calling the setter over and over in a recursive infinite loop, but how does adding the underscore accomplish this?
For reference, I'm using ES6 syntax in a Node.js app.
Original:
set serialNumber(serialNumber) {
this.serialNumber = serialNumber;
}
Corrected:
set serialNumber(serialNumber) {
this._serialNumber = serialNumber;
}