Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
How to issue an error if there is undefined?
"use strict";

class Student {

  #
  name;#
  age;#
  city;#
  programmings;#
  os;


  constructor(name, age, city, programmings, os) {

    this.#name = name;
    this.#age = age;
    this.#city = city;
    this.#programmings = programmings;
    this.#os = os;

  }


  get getName() {

    let hiddenName = '';
    for (let i = 0; i < this.#name.length; i++) {
      hiddenName += '*';
    }
    return 'Student name is ' + hiddenName;
  }


  get getAge() {

    let hiddenAge = '';
    let ageToString = this.#age.toString();

    for (let i = 0; i < ageToString.length; i++) {
      hiddenAge += '*';
    }
    return 'Student age is ' + hiddenAge;
  }

  get getCity() {

    let hiddenCity = '';
    for (let i = 0; i < this.#city.length; i++) {
      hiddenCity += '*';
    }
    return 'Student city is ' + hiddenCity;
  }

  get getProgrammings() {

    let hiddenProgrammings = '';
    for (let i = 0; i < this.#programmings.length; i++) {
      hiddenProgrammings += '*';
    }
    return 'Student programmings is ' + hiddenProgrammings;
  }

  get getOs() {

    let hiddenOs = '';
    for (let i = 0; i < this.#os.length; i++) {
      hiddenOs += '*';
    }
    return 'Student os is ' + hiddenOs;
  }

}


const student = new Student('Ivan', 33, 'Moscow', 'Java', 'Windows');


console.log(student.name);

When accessing any field of an object, the result is directly output "undefined". How to make it so that when accessing the object fields directly, an error is issued, the text of which I will write myself

for example: console.log(student.name);

output: 'Information is private'

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Try this:

console.log(student.name ?? 'Information is private');

?? is an IF statement but in shorter way.

It means if the value is set, the value is true and the value in the left side must be shown, but if the value is not set, the value at the left side must be shown.

about 4 years ago · Juan Pablo Isaza Relatório

0

student.name calls get name() not get getName()

"use strict";
class Student {
    #name;
    #age;
    #city;
    #programmings;
    #os;

    constructor(name, age, city, programmings, os) {
        this.#name = name;
        this.#age = age;
        this.#city = city;
        this.#programmings = programmings;
        this.#os = os;
    }

    get name() {
        return typeof this.#name === "undefined" ? "Information is private" : this.#name;
    }

    get getName() {
        let hiddenName = "";
        for (let i = 0; i < this.#name.length; i++) {
            hiddenName += "*";
        }
        return "Student name is " + hiddenName;
    }

    get getAge() {
        let hiddenAge = "";
        let ageToString = this.#age.toString();

        for (let i = 0; i < ageToString.length; i++) {
            hiddenAge += "*";
        }
        return "Student age is " + hiddenAge;
    }

    get getCity() {
        let hiddenCity = "";
        for (let i = 0; i < this.#city.length; i++) {
            hiddenCity += "*";
        }
        return "Student city is " + hiddenCity;
    }

    get getProgrammings() {
        let hiddenProgrammings = "";
        for (let i = 0; i < this.#programmings.length; i++) {
            hiddenProgrammings += "*";
        }
        return "Student programmings is " + hiddenProgrammings;
    }

    get getOs() {
        let hiddenOs = "";
        for (let i = 0; i < this.#os.length; i++) {
            hiddenOs += "*";
        }
        return "Student os is " + hiddenOs;
    }
}

const student = new Student("Ivan", 33, "Moscow", "Java", "Windows");

console.log(student.name);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda