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

96
Views
Una función está mutando un objeto antes de ser llamada: ¿cómo es posible?

Tengo una pregunta js bastante extraña. Tengo un componente Vue (pero creo que Vue no es importante aquí). Tiene una propiedad de datos llamada current_query . En un método que responde a un evento, quiero modificar esa propiedad. Usando Lodash aquí. Y hay un comportamiento realmente curioso. Digamos que this.current_query es inicialmente == { test: 500 } . Di también que el parámetro 'field' es 'test' . Al 'desconfigurarlo' de current_query , de hecho, vaciaré current_query. Perfecto. La cuestión es que (solo) cuando uso unset, si registro current_query antes de llamar a unset, obtendré un objeto vacío. ¿Como es posible?

 on_applied_option_click(field){ console.log(this.current_query); // gives { test: 500 } (OK) }, // end click on_applied_option_click(field){ console.log(this.current_query); // gives empty object only if, and when, using _.unset (??) _.unset(this.current_query, field); console.log(this.current_query); // gives empty object as well (OK) }, // end click

Descubrí que si uso _.omit en su lugar, todo va bien:

 on_applied_option_click(field){ console.log(this.current_query); // gives { test: 500 } (OK) this.current_query = _.omit(this.current_query, field); console.log(this.current_query); // gives empty object, which at this point is fine }, // end click

¿Por qué? Lo único que puedo decir es que _.unset muta los objetos mientras que _.omit crea otros nuevos. Pero aún así, creo que es bastante imposible que _.unset vaya a mutar el objeto incluso antes de que lo llamen (???). Nunca había visto una cosa como esta. ¿Alguien tiene alguna buena explicación de este extraño comportamiento?

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

0

En lugar de hacer console.log(this.current_query) , intente console.log(JSON.stringify(this.current_query)) . Eso registra de manera confiable el estado de this.currenty_query en el momento del registro, y no posiblemente algún estado posterior.

Y eso debería resolver la confusión.

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!