www.webdeveloper.com
Results 1 to 8 of 8

Thread: [RESOLVED] Parameters in Function Pointers

  1. #1
    Join Date
    Mar 2010
    Posts
    48

    resolved [RESOLVED] Parameters in Function Pointers

    Hello guys,

    I have been struggling with function pointers on javascript.
    I'm trying to use variables inside my function pointer but I'm not able to do it.

    What i'm trying to do is something like this:

    Code:
    param1= 2;
    
    function(param1)
    {
         someFunction(param1);
    }
    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?

    Is it even possible?

    Thank you so much in advance for any input!

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Code:
    param1= 2;
    
    (function(param1)
    {
         someFunction(param1);
    }(param1))

  3. #3
    Join Date
    Dec 2010
    Posts
    207
    Quote Originally Posted by rnd me View Post
    Code:
    param1= 2;
    
    (function(param1)
    {
         someFunction(param1);
    }(param1))
    Do those parentheses have any effect or did you mean
    Code:
    param1 = 2;
    
    (function(param1)
    {
         someFunction(param1);
    })(param1);
    ?

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    6 of 1 half-dozen of the other.
    i did it my way before everyone decided to do it the other way i swear...

  5. #5
    Join Date
    Jul 2003
    Location
    The City of Roses
    Posts
    2,503
    @mavalos88

    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.)
    for(split(//,'))*))91:+9.*4:1A1+9,1))2*:..)))2*:31.-1)4131)1))2*:3)"'))
    {for(ord){$i+=$_&7;grep(vec($s,$i++,1)=1,1..($_>>3)-4);}}print"$s\n";

  6. #6
    Join Date
    Mar 2010
    Posts
    48
    Quote Originally Posted by Jeff Mott View Post
    @mavalos88

    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.

    Thank you everyone for your help and insight!

  7. #7
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    Quote Originally Posted by mavalos88 View Post
    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.

    naive loop/function combo says "3" 3 times:
    Code:
    for(x=0; x<3;x++){
      setTimeout(function(){ alert(x);}, 1000 * x);
    }

    but using a scope-gate (in red), it's what you expect (0,1,2):
    Code:
    for(x=0; x<3;x++){
    (function(x){
      setTimeout(function(){ alert(x);}, 1000 * x);
    }(x));
    }

  8. #8
    Join Date
    Mar 2010
    Posts
    48
    rnd me, just tried it and it worked!
    It would have never crossed my mind to try it that way.

    Thank you very much! You are a life saver!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles