Jump to content

Pass Data As Parameter Or Store As Global Var?


johnnyg24

Recommended Posts

Can someone tell me if one way is more efficient than another?I would like to know if passing data from function1 to function2 as a parameter is more efficient than storing the data as a global variable and having function2 access the data that way? Function2 is the only function on the page that will be using the data, I know I don't have to store the data as a global variable if only one function is using it but I don't know if it's more efficient than passing it as a parameter.Thank you!

Link to comment
Share on other sites

I would say try and be as precise as possible as far as scoping goes. It will make things easier in the event you need to change your code for any reason, and is good practice in general. If only function2 will be using the data, I would just pass it.

Link to comment
Share on other sites

Passing the data as a parameter seems more straight forward. However, I have run into one problem with that method. Even though function2 is the only function that utilizes the data, it can be called a number of times from the same page. When function2 is run a second time, the data is no longer being passed and therefore not available.

Link to comment
Share on other sites

you could use some sort of condition checker at the beginning of the function to check the value of the argument being passed and have the function act accordingly. Make it run its code only if the argument passed matches a certain condition, else, blah.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...