Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Aug 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Anonymous Function Variable Name to String

    Hi, I hope you can help me.
    But I think this is near on impossible.

    The code, cut down:

    Function funcMain(funcNumber){
    ... other code
    //First time round, this next statement calls funcOne.
    //Second time, it calls funcTwo.

    funcNumber("Hello");
    }
    funcOne = Function(argHello) {
    //Code where argHello is used and where I want to be able to
    //get the string "funcOne" from the very fact that this is the funcOne
    //function, if it is possible.

    ... code
    }
    funcTwo = Function(argHello) {
    //Same as funcOne.
    ... code
    }
    //First, call function funcMain and pass the anonymous function funcOne.
    funcMain(funcOne);
    //Second, call function funcMain and pass the anonymous function funcTwo.
    funcMain(funcTwo);

    I could add a string argument to the funcMain call, but that would not be as swift as the way I want it.

    Please don't question my reasoning or whether I could do all this in a different way.
    I'm only after one thing, whether it is possible to extract the variable name, that was assigned an anonymous function and use it as a string. (in this case funcOne or funcTwo)

    I hope I have made sense, but it is quite difficult to explain.

    ta muchly,

    David Smith

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Do you mean like this?


    <script>
    <!--
    first_run=1
    function funcMain(){
    if(first_run==1){
    funcOne("Hello");
    first_run=0
    }
    else{
    funcTwo("World");
    }
    }

    function funcOne(argHello) {
    alert(argHello)
    }

    function funcTwo(argHello) {
    alert(argHello)
    }

    funcMain();
    funcMain();
    // -->
    </script>


  •  

    Posting Permissions

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