If someFunction tries to use param1, it will literally try to use "param1" instead of it's value of 2 (on the above example).
The last thing I tried was what i mentioned above. If i don't pass a parameter to the function pointer I get the same result, I can't use the value of param1.
The only way I got it to work is if I make param1 a global variable.
The problem is that i'm running my function pointer inside a for loop, so the value of param1 changes every time. If i wanted to try the global variable solution, i'd have to make an array of values in order for this to work, which I can't afford to do.
Do you guys know of a way to pass parameters into a function pointer without using global variables?
First, about the code snippet you posted. The first thing you do is make a global variable named param1. Then you make a function that takes a parameter, and that parameter is also named param1. So inside that function, the parameter hides the global variable. That may be the cause of some of your confusion.
Next, you said you're running your function inside a loop, and that can complicate things in JavaScript. It's critical to know if you're merely calling the function or if you're actually creating the function from inside the loop. The only way we can know for sure is if you show us more of your code. (Actually all your code would be best, because you never know what might be relevant.)
First, about the code snippet you posted. The first thing you do is make a global variable named param1. Then you make a function that takes a parameter, and that parameter is also named param1. So inside that function, the parameter hides the global variable. That may be the cause of some of your confusion.
Next, you said you're running your function inside a loop, and that can complicate things in JavaScript. It's critical to know if you're merely calling the function or if you're actually creating the function from inside the loop. The only way we can know for sure is if you show us more of your code. (Actually all your code would be best, because you never know what might be relevant.)
I'm creating the function inside the loop for later execution.
I think the problem was that the parameters i'm passing to the function stop existing by the time I execute the function pointer and therefore no value can be used.
I'm going to use a different method because function pointers will not work for me because of what I mentioned.
I'm creating the function inside the loop for later execution.
I think the problem was that the parameters i'm passing to the function stop existing by the time I execute the function pointer and therefore no value can be used.
I'm going to use a different method because function pointers will not work for me because of what I mentioned.
Thank you everyone for your help and insight!
functions can see outer variable even after they no longer exist; it's called closure. i know it seems like magic, but it works simply and effectively in practice.
if you wrap the variable you need to transport in an anon function, like the pattern i gave, you can use another function inside to memorize the variables for later.
i call the pattern a "scope gate", but there might be another name for it.
Bookmarks