johnnyg24 Posted December 18, 2009 Share Posted December 18, 2009 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 More sharing options...
thescientist Posted December 18, 2009 Share Posted December 18, 2009 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 More sharing options...
johnnyg24 Posted December 18, 2009 Author Share Posted December 18, 2009 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 More sharing options...
roundcorners Posted December 18, 2009 Share Posted December 18, 2009 how is it called the other times?You'll need to initiate it with the same arg Link to comment Share on other sites More sharing options...
thescientist Posted December 18, 2009 Share Posted December 18, 2009 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.