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

174
Views
¿El cambio de objeto en js también es local en una función?

El siguiente genera un archivo que contiene código e imprime la salida del programa:

 class Code { static list = require("./settings.json"); static fs = require("fs"); constructor( code="print('Hello World')",//default code lang = "py",//language fn = "test",//file name args = "" //arguments ) { this.code = code; const path = require('path'); this.fn = path.join(__dirname,fn) + "." + lang;//for platform independence this.run_com = Code.replace(Code.list[lang], this.fn, args); this._output="Server not responding"; this._error="Server not responding"; } file_exists() { return Code.fs.existsSync(`${this.fn}`); } make_file() { Code.fs.writeFile(`${this.fn}`, this.code, (err) => { if (err) { console.log(err); } }); } run_file() { if(!this.file_exists()) this.make_file(); const { exec } = require("child_process"); exec(this.run_com, (error, stdout, stderr) =>{ if(error) { console.log(error); } else if(stderr==""){ this._output=stdout; this._error="No error"; } else{ this._error=stderr; this._output=stdout; } console.log(this);//this }); console.log(this);//this } get error() { return this._error; } get output() { return this._output; } static replace( s //replace {} ) { const arg = Object.values(arguments).slice(1); //convert obj to array removing s for (let i in arg) { s = s.replace("{}", arg[i]); } return s; } } a = new Code("print('exd')"); a.run_file();

En el programa anterior, la salida es: ingrese la descripción de la imagen aquí

El cambio en el estado del objeto no es persistente una vez que sale de la función interna.

por qué el segundo console.log se imprime primero y por qué la salida de ambos console.log es diferente, por favor justifique en detalle.

También sugiera una solución a este problema.

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

0

Creo que esto se debe a que exec es asíncrono y el segundo registro se imprime primero. Cuando se ejecuta el código asíncrono, se imprime el segundo registro. Y en el exec, los valores de _output y _error se cambian, de ahí la diferencia.

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!