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 6 of 6
  1. #1
    New Coder
    Join Date
    Sep 2008
    Posts
    48
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Keeping variables between functions

    I've noticed that if you declare a variable in one function, then call another function, the variables cannot be accessed in that function. Is there any way to get the value of a variable declared in a different function?

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    Well if you declare them in the function they are local.

    Make them global? Learn about OOP?

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by blitz104 View Post
    I've noticed that if you declare a variable in one function, then call another function, the variables cannot be accessed in that function. Is there any way to get the value of a variable declared in a different function?
    no. as you describe and assuming un-nested functions, absolutely not.
    not unless you make them available in some other fashion.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,022
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Local variables exist only inside a particular function, hence they said to have Local Scope. Global variables on the other hand are present throughout the script and their values can be accessed by any function. Thus, they are said to have Global Scope.

    A variable is global if
    a) it is declared with or without the var keyword outside a function
    b) it is declared without the var keyword within a function.

    A variable is local if it is declared with the var keyword within a function.

    It is possible to have two variables in a script with the same name, one global and one local.
    Obviously this is very undesirable and not recommended.

    Code:
    <script type = "text/javascript">
    var x = 10;
    alert (x);  // x is global
    y = 5;  // y is also global
    function show() {
    var y = 20;  // this y is local
    alert (y);
    }
    
    show()
    </script>
    Last edited by Philip M; 12-13-2009 at 08:53 AM.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    If you don't yet know OOP, my advise is to namespace the functions and store the needed variables in the namespace:

    Code:
    var Foo = {};
    
    Foo.x = 1;
    
    Foo.abra = function () {
      Foo.x = Foo.x * 2;
      return 1;
    };
    
    Foo.kadabra = function () {
      return Foo.x;
    };
    
    alert (Foo.abra);
    alert (Foo.kadabra);
    alert (Foo.abra);
    alert (Foo.kadabra);
    alert (Foo.abra);
    alert (Foo.kadabra);
    Last edited by Trinithis; 12-14-2009 at 07:32 AM.
    Trinithis

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,349
    Thanks
    11
    Thanked 589 Times in 570 Posts
    Quote Originally Posted by Trinithis View Post
    If you don't yet know OOP, my advise is to namespace the functions and store the needed variables in the namespace:
    at the slight cost of speed, you can code this more succinctly using with:


    Code:
    //pre define all vars here:
    var Foo = {x:1};
    
    //your functions inside a with can share named properties like they are vars.
    with(Foo){
    Foo.abra = function () {
      x = x * 2;
      return 1;
    };
    
    Foo.kadabra = function () {
      return x;
    };
    
    alert (abra());
    alert (kadabra());
    alert (abra());
    alert (kadabra());
    alert (abra());
    alert (kadabra());
    }//end with 
    note that these fake vars can be set, but not created by name.
    if a property doesn't exist and you just use the name, you will create a global.
    you can manually create new "vars" by adding them onto the namespace from anywhere (Foo in this case).
    Last edited by rnd me; 12-14-2009 at 12:07 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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