0

I have a question about variable scope in NodeJS. Lets assume that we have a function (MainFunc) that calls another function (SecondFunc). The second function has a callback:

function MainFunc(arg1, arg2, arg3)
{
  SecondFunc(arg1, (respondJSON)
  {
    //AM I ALLOWED TO DO THE FOLLOWING?
    respondJSON.name = arg2;
    respondJSON.age = arg3;
  }
}

As you can see my sample code, arg2 and arg3 are used inside the callback body. Am I allowed to do this? Because the MainFunc might be called several time a second by many users, Are arg2 and arg3 GUARANTEED to be the same as the arguments or might be has another value that is call by another user, just before the callback is called?

Bardia
  • 155
  • 1
  • 2
  • 13
  • In a nutshell: yes, this is fine. – deceze Oct 01 '18 at 12:24
  • Thanks for the answer. my question might be a bit different (at least from my viewpoint) from the aforementioned question. I wanted to know what happens to argument function if you use it inside a callback function. – Bardia Oct 01 '18 at 12:48
  • You might want to read more about closures then; I've added some more duplicates. – deceze Oct 01 '18 at 12:49

0 Answers0