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

226
Views
i dont know why the functions aren´t using my variable

i made a variable with a string inside, an then put that variable in two different functions. The problem here is that it shows me an error: "Uncaught ReferenceError: mostrarDatosTexto is not defined". I´m not sure enough why is this happening. Here´s the code:

const Libro = (titulo, autor) => {return(

    {
        autor: autor, 
        titulo: titulo,
        mostrarDatosTexto: `${titulo}, de ${autor.toUpperCase()}`,
        mostrarDatosEnConsola:  () => {return (console.log(mostrarDatosTexto))},
        mostrarDatosEnAlert:    () => {return (alert(mostrarDatosTexto))},
    }

)}


let unLibro = Libro('Ángeles y Demonios', 'Dan Brown')

console.log(unLibro)

unLibro.mostrarDatosEnConsola()
unLibro.mostrarDatosEnAlert()

i'll really appreciate it if you can help me

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

0

mostrarDatosTexto is a property, not a variable. You need to access it using XXX.mostrarDatosTexto syntax.

Inside an object method, you can use this to refer to the object that the method was called on. But you have to use an ordinary function, not an arrow function, because arrow functions preserve the value of this from the scopre where they were defined, they don't receive it as the method call contact.

console.log() and alert() don't return anything, so there's no point in using return to return their values. Just call them without using return.

const Libro = (titulo, autor) => {
  return (
    {
      autor: autor,
      titulo: titulo,
      mostrarDatosTexto: `${titulo}, de ${autor.toUpperCase()}`,
      mostrarDatosEnConsola: function() {
        console.log(this.mostrarDatosTexto);
      },
      mostrarDatosEnAlert: function() {
        alert(this.mostrarDatosTexto);
      },
    }
  )
}


let unLibro = Libro('Ángeles y Demonios', 'Dan Brown')

console.log(unLibro)

unLibro.mostrarDatosEnConsola()
unLibro.mostrarDatosEnAlert()

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!