Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

293
Views
¿Cómo emitir un error si no está definido?
"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);

Al acceder a cualquier campo de un objeto, el resultado se muestra directamente como "indefinido". Cómo hacer que al acceder directamente a los campos del objeto, se emita un error, cuyo texto escribiré yo mismo

por ejemplo: consola.log(estudiante.nombre);

salida: 'La información es privada'

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Prueba esto:

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

?? es una sentencia IF pero de forma más corta.

Significa que si se establece el valor, el valor es verdadero y se debe mostrar el valor del lado izquierdo, pero si no se establece el valor, se debe mostrar el valor del lado izquierdo.

about 4 years ago · Juan Pablo Isaza Report

0

student.name llama get name() no 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!