0

First of all, I'm sorry for asking this which was seemingly asked before, but... In the questions I found, people talk about scope (and I'm familiar with the concept) or about some "callback" or "asynchronous" things that I don't really understand. The reason why I ask this is because all my global variables so far, were normally affected by the great number of functions. The code I have is way too big for me to paste it here, but I'll give you an example.

var ContainsSomeNumber = 50;
var AnObjectWithSomeValues ...
var TheFunctionInQuestion = function(nameRepresentingTheObject, nameRepresentingNumberVar){
   if(nameRepresentingTheObject.lestSayName ==== "SomeString"){
      nameRepresentingNumberVar+=5;
   }
}

Now the thing is, it understands both variables and gets the correct result when I pass them into the function, however, the ContainsSomeNumber variable remains unaffected by the function on a global scale. If I place an alert or log to show me how much it is inside the function, it shows 55, as it should. But once I try to access the modified global variable in another place with the modified value after the function has been run successfully (chrome and firefox show no foul play), it returns the same unmodified value of 50. Is the problem because I didn't use hardcode versions of variables to pass in the function, or what? Because like I said, it sees and modifies it the right way, but only inside the function. Shouldn't scope see the variable as a global one as I didn't write "var" in front of the one I used in the function? I mean, I didn't declare it as a new variable, plus it took it's global value normally. Why doesn't the global variable stay modified by the function?

I hate JS
  • 35
  • 5
  • Are you actually executing the function before checking the variable's value – AM Douglas Oct 30 '16 at 18:07
  • @amdouglas I do, and when I check it inside the function itself, it is changed, but right after the function is executed, I check in through alert or log in another place and it appears it's unaffected. – I hate JS Oct 30 '16 at 18:10
  • @IhateJS Read the duplicate... this is a principal of many languages. – Andrew Li Oct 30 '16 at 18:15
  • @Andrew Li Ok, although the function passes the object and successfully checks it's given value. The other variable is simple "primitive" type, number. And that number value is what I need to change not the object. But ok. – I hate JS Oct 30 '16 at 18:43

1 Answers1

0

Javascript is always pass by value, but when a variable refers to an object (including arrays), the "value" is a reference to the object. So if you want to a function modify an variable outside there scope, you should pass an object and change some attribute inside the object.

var obj = {
  ContainsSomeNumber: 50;
}
var AnObjectWithSomeValues ...
var TheFunctionInQuestion = function(nameRepresentingTheObject, obj){
   if(nameRepresentingTheObject.lestSayName === "SomeString"){
      obj.ContainsSomeNumber+=5;
   }
}

Or you can return the value:

var ContainsSomeNumber = 50;
var AnObjectWithSomeValues ...
var TheFunctionInQuestion = function(nameRepresentingTheObject, nameRepresentingNumberVar){
   if(nameRepresentingTheObject.lestSayName === "SomeString"){
      nameRepresentingNumberVar+5;
   }
   return nameRepresentingNumberVar
}
lucas.coelho
  • 894
  • 1
  • 9
  • 16