title: Does the change of object in js is also local in a function?
body: <p>The bellow generates a file containing code and prints the output of the program:</p>
  <pre><code> class Code {
    static list = require(&quot;./settings.json&quot;);
    static fs = require(&quot;fs&quot;);
    constructor(
      code=&quot;print('Hello World')&quot;,//default code
      lang = &quot;py&quot;,//language
      fn = &quot;test&quot;,//file name
      args = &quot;&quot; //arguments
    ) {
      this.code = code;
      const path = require('path');
      this.fn = path.join(__dirname,fn) + &quot;.&quot; + lang;//for platform independence
      this.run_com = Code.replace(Code.list[lang], this.fn, args);
      this._output=&quot;Server not responding&quot;;
                this._error=&quot;Server not responding&quot;;
    }
  
    file_exists()
    {
      return Code.fs.existsSync(`${this.fn}`);
    }
  
    make_file() {
      Code.fs.writeFile(`${this.fn}`, this.code, (err) =&gt; {
        if (err) {
          console.log(err);
        }
      });
    }
  
    run_file()
    {
        if(!this.file_exists())
        this.make_file();
  
        const { exec } = require(&quot;child_process&quot;);
        
        exec(this.run_com, (error, stdout, stderr) =&gt;{
            if(error)
            {
                console.log(error);
            }
            
            else if(stderr==&quot;&quot;){
            this._output=stdout;
            this._error=&quot;No error&quot;;
        }
  
            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(&quot;{}&quot;, arg[i]);
      }
      return s;
    }
  }
  
  a = new Code(&quot;print('exd')&quot;);
  a.run_file();
  </code></pre>
  <p>In the above program the output is:
  <a href="https://i.stack.imgur.com/PcSIS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/PcSIS.png" alt="enter image description here" /></a></p>
  <p>The change in the state of object is not persistent once it comes out of the inner function.</p>
  <p>why does the second console.log prints first and why the output of both the console.log are different, please justify in detail.</p>
  <p>Also suggest a solution of this problem.</p>