Roll20 uses cookies to improve your experience on our site. Cookies enable you to enjoy certain features, social sharing functionality, and tailor message and display ads to your interests on our site and others. They also help us understand how our site is being used. By continuing to use our site, you consent to our use of cookies. Update your cookie preferences .
×
Create a free account

Passing objects to a function...

Basically, I want to create functions that I can call in multiple places within a script as needed (due to timing issues with certain things) , but I'm not quite sure this is how it works... obj = findObjs(blah blah) checkHealth(obj); function checkHealth(obj) {   do stuff here }
1368184921
Alex L.
Pro
Sheet Author
Take a look at my code and you will see how i have done it.
In that example, I would name the inner "obj" variable in the checkHealth() function to something else, e.g. checkHealth(thisobj). The reason for that is the way that Javascript works the scope outside the function is available inside the function. So if you access the obj variable inside the checkHealth function you would probably be accessing the one passed to the function instead of the one on the first line, but it can be confusing and prone to error. By using "thisobj" (or some other name) you can clearly know which object you're referencing. Other than that, the thisobj variable inside the checkHealth function is the same as the obj variable you got from findObj...you can do any/all of the same things to it (set(), get(), etc.). Do note as well that Javascript functions are synchronous by default, meaning that when you call a function all of the statements in that function will execute before the script continues with the next line after the function call.