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

84
Views
A function is mutating an object before being called: how is it possible?

I've got a quite strange js question. I've got a Vue component (but I think Vue is not important here). It has a data property called current_query. On a method that responds to an event, I want to modify that property. Using Lodash here. And there's a really curious behaviour. Say this.current_query is initially == { test: 500 }. Say also that 'field' parameter is 'test'. By 'unsetting' it from current_query, in fact I'll empty current_query itself. Perfect. Thing is, that (only) when using unset, if I log current_query before calling unset, I'll get an empty object. How is it possible?

  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

I've found out that if I use _.omit instead, it all goes fine:

  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

Why? The only thing I can say is that _.unset mutates the objects while _.omit creates new ones. But still, I think it's quite impossible that _.unset is going to mutate the object before even getting called (???). Never seen a thing like this. Has anyone some good explanation of this strange behaviour?

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

0

Instead of doing console.log(this.current_query), try console.log(JSON.stringify(this.current_query)). That logs reliably the state of this.currenty_query at the time of logging, and not possibly some state afterwards.

And that should solve the confusion.

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!