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

115
Views
Los objetos de una fábrica devuelven diferentes valores de propiedad dependiendo de dónde se acceda a ellos

Mi objetivo es crear un nuevo objeto de trabajo a la vez con un estado activo de verdadero, y luego establecer el estado activo de los últimos objetos de trabajo creados en falso.

Estoy intentando hacer esto usando Object.assign() en una fábrica de objetos (disculpas si esta no es exactamente la terminología correcta).

El problema que tengo es que la propiedad state.active se actualiza y es correcta en la función gen_job(), PERO es incorrecta cuando se lee dentro de la función update_all_jobs().

Me gustaría hacer esto sin clases o usando la palabra clave 'esto' como ejercicio para mejorar mi comprensión del idioma.

Muchas gracias si alguien puede arrojar algo de luz sobre lo que estoy haciendo mal aquí.

Ejemplo mínimo, también en Codepen.io :

 let job_counter = 0; // helps with debugging let jobs = []; // an array to store job objects // Return an object with an 'active' property set to true const create_job = (x, y) => { // define some state properties for the job object let state = { active: true, ox: x, oy: y, job_id: job_counter++, }; return Object.assign({}, state, updater(state)); }; const updater = function (state) { return { update: function () { console.log( `Inside update_all_jobs(). Job_id: ${state.job_id}, state.active is ${state.active}` ); } }; }; const gen_job = () => { // test to see if there is a previously created job object, and if true, // set the last created object's active state to false if (jobs.length > 0) { jobs[jobs.length - 1].active = false; } jobs.push(create_job(10, 20)); jobs.forEach((jb) => console.log( // works here!? `Inside gen_job(): Job_id: ${jb.job_id}, active state: ${jb.active}` ) ); }; // This is called after a new job is created. const update_all_jobs = function () { jobs.forEach((jb) => { jb.update(); }); }; gen_job(); update_all_jobs(); gen_job(); update_all_jobs(); // Console output // /* "Inside gen_job(): Job_id: 0, active state: true" "Inside update_all_jobs(). Job_id: 0, state.active is true" "Inside gen_job(): Job_id: 0, active state: false" "Inside gen_job(): Job_id: 1, active state: true" "Inside update_all_jobs(). Job_id: 0, state.active is true" "Inside update_all_jobs(). Job_id: 1, state.active is true" */ // Why does Job_id 0 switch from false to true??
about 4 years ago · Juan Pablo Isaza
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!