Return Json with the attributes of a class without "_" in the initials

Asked

Viewed 56 times

0

have this following class javascript:

class Usuario {
  constructor() {
    this._nome = '';
    this._idade = '';
  }


  get nome() {
    return this._nome;
  }

  set nome( value ) {
    this._nome = value;
  }

  get idade() {
    return this._idade;
  }

  set idade( value ) {
    this._idade = value;
  }
}

When I return she saw json, she returns me the names with _ at first. I know this sign means that the attribute can only be used within the class itself, so I’d like to return the attribute names without this character.

  • 1

    How about taking the _ that it’s just noise in the name?

  • But good practice says it’s good to encapsulate the data.

  • This is completely absurd. Read some of these answers: https://answall.com/search?q=user%3A101+boas+pr%C3%A1ticas I have a talk about the harm people are doing to others and themselves with this good practice business.

  • You may be right, but the question isn’t about good practice.

  • Who argued about good practice was you, I just replicated what you said. I gave you an easier and more correct solution, I would do the right thing, but it’s your right not to do it.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.