Are new instances created when calling the same function, or are they the same? That is, for example, if in this code g () is executed after the second call of f (), will the new call affect the variable "a", how does g () see it?

function f(value) { var a = value; somePromise().then(() => { ... g(); }); } f(1); f(5); 

    1 answer 1

    For each function f () call, there will exist its own local variable a, which will fall into the context of the closure:

     function g(a) { document.getElementById('log').innerHTML += 'g(' + a + ')<br />'; } function f(value) { var a = value; setTimeout(() => { g(a); }, 500); } f(1); f(5); 
     <p id='log'></p>