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 9 of 9
  1. #1
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts

    How do I set a variable name from a function parameter

    Hi

    Is is possible to set a variable name from a function parameter?

    Kind of like this but it doesn't work!

    Your help is greatly appreciated.

    Thanks

    Code:
    function Load(setVar){ 
    	
            setVar="test1";
    
    	alert(M);
    }
    
    Load(M);

  • #2
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Do you have some more explanation of what it is that you're trying to do? Just in plain English, what are you trying to do?

  • #3
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    thanks for the reply

    I'm trying to set a variable name through the parameter in the calling function



    eg in the script below

    1)Load(M) will insert M where setVar is.




    Code:
    Load(M);
    
    function Load(setVar){ //setVar is set to  M
    	
    setVar="test1"; //setVar becomes M
    
    alert(M);//"test1" alerts
    alert(Z);//wont alert because it does not exist
    }

  • #4
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You're talking global variables here, right? They can be accessed through the window object.

    Here's your modified code; please mind the last comment:

    Code:
    Load('M');
    
    function Load(setVar){ 
    	
    	window[setVar]="test1"; //gobal variable M is set to "test1"
    	
    	alert(M);//alerts "test1"
    	alert(Z);//will not only not alert, but throw an exception and halt script execution, since Z is not defined
    }

  • Users who have thanked venegal for this post:

    andrew1234 (07-19-2009)

  • #5
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Hi Thanks so much ... thats it


    so I see if I replace window to this.

    it also works

    but does it mean that its only for inside that function --like not using the var?

    Thanks so much for the help . I really appreciate it.


    Code:
    Load(M);
    
    function Load(setVar){
    	
    this[setVar]="test1"; 
    
    alert(M);
    
    }

  • #6
    GŁtkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    No, the variable M is not scoped within the function, it's global (if that's what you mean). You can test that by just putting the alert outside of the function.

    In fact using "this" instead of "window" here does not make any difference at all, since "this" within a global function refers to the window object.

    You have an error in your code though, it has to be Load('M') and not Load(M).

  • Users who have thanked venegal for this post:

    andrew1234 (07-19-2009)

  • #7
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    thanks again. you really helped me

  • #8
    Regular Coder
    Join Date
    Mar 2008
    Posts
    301
    Thanks
    2
    Thanked 30 Times in 30 Posts
    Quote Originally Posted by andrew1234 View Post
    thanks again. you really helped me
    You're going to think it a dumb question - I realize.

    But, uh . . why would you want to do that?

  • #9
    Regular Coder
    Join Date
    Oct 2002
    Posts
    147
    Thanks
    31
    Thanked 2 Times in 2 Posts
    Hi FSIDE sorry for the late reply

    To answer you question

    Just wanted to know if it could be done and maybe because...


    one function gets called twice but each time it must behave at a differently but this function is running twice at the same time.

    so I thought that if I choose the Variable names in the peratmter then it would not get confused...

    but

    I have not learned about making my own objects yet. so I think they might be a better way.

    any ideas?

    Thanks

    Andrew


  •  

    Posting Permissions

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