0

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;
    }
scoffin
  • 35
  • 1
  • 12
  • 1
    underscore as a prefix on a field name is often used to denote that the field is private. It is a syntax convention, to mark it different from associate property – apomene Feb 10 '20 at 15:04

4 Answers4

1

It has no special meaning, but it makes the property name distinct.

The setter set serialNumber handles calls to set the serialNumber property. If you try to set that same property inside the setter, then the setter is calling itself recursively and will never terminate.

This is one of a number of conventions to name a "private" data property intended for use within the class, but it has no particular significance to the JS runtime.

Jeremy Roman
  • 16,137
  • 1
  • 43
  • 44
0

The underscore itself does nothing. There's no special meaning to it. All it does is create a different name. Now instead of setting the value to serialNumber, you're setting it to _serialNumber. It's just a different name.

What this means for your specific case is that you're not calling your setter recursively. this.serialNumber = .. invokes the setter set serialNumber. Which then invokes the setter set serialNumber. Which then invokes the setter set serialNumber. Which then invokes the setter set serialNumber. Ad infinitum…

deceze
  • 510,633
  • 85
  • 743
  • 889
0

Assignments to properties are not handled differently inside the setter method than outside. So if the setter tries to assign the same property as the name of the setter, it will call the setter again, causing infinite recursion.

To get around this, the setter has to set a different property. The underscore prefix is just a common naming convention to distinguish the internal property name from the visible property.

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

You understand very badly :)

There is no relationship of _ with logic, it is just a notation.

User _ for private variables and property name without underscore, for example for age, gender

class Person {
  private _age: number;
  private _gender: string;

  get age() { return this._age }
  set age(personAge: number) { this._age = personAge }

  get gender() { return this._gender }
  set age(personGender: string) { this._gender = personGender }
}

Hope it will help you to understand the concept of _.

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85