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

111
Views
La variable global utilizada en la propiedad del objeto no se actualiza dentro del objeto - javascript

Tengo un objeto configurado así:

 let xVal = 0; const myObj = { infoText: "The value of x is: " + xVal }

Se hace referencia a este texto de información más adelante para mostrar una especie de información sobre herramientas al pasar el cursor sobre un elemento.

 function showTooltip(obj) { targetEl = document.getElementById('targetId'); targetEl.innerHTML = obj.infoText; }

El valor de xVal puede cambiar. El problema con el que me encuentro es que, independientemente de si ese valor ha cambiado o no, cuando paso el mouse sobre mi elemento, siempre mostrará el valor inicial de xVal. Supongo que esto se debe a que myObj.infoText se establece cuando se inicia y extrae el valor inicial de xVal. ¿Cómo puedo obtener myObj.infoText para mostrar el xVal actual y mantenerme al día con los cambios?

EDITAR: aquí hay un mejor ejemplo de mi código, me doy cuenta de que originalmente ofrecí una muestra deficiente.

 let count = 0; let clickVal = 1; const myObj = { cost: 10, infoText: `This displays the current value of your click <br /> Current click = ` + clickVal }; function cardContent(obj) { targetEl = document.getElementById('targetId'); targetEl.innerHTML = obj.infoText; } cardContent(myObj); // initialize card content function handleClick() { count += clickVal; } function upgradeClick() { count -= myObj.cost; clickVal += 1; cardContent(myObj) // attempting to update the text to reflect new value }

Hay un botón que invoca handleClick y otro que invoca upgradeClick . Usando CSS, el siguiente div está oculto y se muestra al pasar el mouse sobre el botón upgradeClick . <div id='targetId'></div> Aquí es donde se muestra el infoText .

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

0

Puedes usar algo como un getter

 let xVal = 0; const myObj = { get infoText() {return "The value of x is: " + xVal;} } function showTooltip(obj) { console.log(obj.infoText); } showTooltip(myObj); xVal = 29; showTooltip(myObj); xVal = 794; showTooltip(myObj);

about 4 years ago · Juan Pablo Isaza Report

0

Crear el objeto cada vez dentro de showTooltip debería funcionar

 let xVal = 0; function showTooltip() { const myObj = { infoText: "The value of x is: " + xVal } console.log(myObj.infoText); } showTooltip(); xVal = 50; showTooltip();

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!