Results 1 to 6 of 6

Thread: global variable scope within function

  1. #1
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default global variable scope within function

    hiy
    just a question
    do global variables defined inside functions get destroyed when the function ends?

    just to be sure i got this right, by global variable inside a function i mean this:

    function myFunction() {
    myVar = 500;
    }

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No. But that's an undeclared global variable that could lead to conflicts with other scripts, or with markup (the document object) in some browsers.

    To avoid the first problem there are a number of things you can do. One would be to assign it as a property of the function:

    Code:
    function myFunction() {
    myFunction.myVar = 500;
    }
    Then it would be available outside myFunction as:

    Code:
    myFunction.myVar
    Another way would be to put the function itself inside another object (a function or a simple javascript object). That gets a little complicated and has various ways it can be done depending upon personal choice and the code objectives. If you want to learn about that, give a more concrete example.

    Both of the above approaches will also solve the second issue, which can be avoided without them (assuming this script runs on a page in a window as almost all javascript does) by declaring it as a property of the window object (the window is the global object in that case):

    Code:
    function myFunction() {
    window.myVar = 500;
    }
    But just in case it doesn't, you can simply declare it outside the function:

    Code:
    var myVar;
    function myFunction() {
    myVar = 500;
    }
    This latter is probably better than the window.myVar method because it will avoid some script errors in cases where myVar gets accessed before myFunction has run.
    Last edited by jscheuer1; 04-12-2011 at 03:50 PM. Reason: add last line
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    ricmetal (04-12-2011)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    John - a related question:

    I was under the impression that declaring a variable, using the var keyword (construct, whatever it is) would keep it local:
    Code:
    function myFunc(){
       var myVar = 500 /* local variable */
       myVar2 = 600 /* global variable */
    }
    is this correct?

  5. #4
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    thanks jscheuer1

  6. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Quote Originally Posted by traq View Post
    John - a related question:

    I was under the impression that declaring a variable, using the var keyword (construct, whatever it is) would keep it local:
    Code:
    function myFunc(){
       var myVar = 500 /* local variable */
       myVar2 = 600 /* global variable */
    }
    is this correct?
    Yes. The same caveats previously mentioned apply to the global there. It's an undeclared global variable. In some IE (all perhaps), if you also have:

    Code:
    <div id="myVar2">Whatever</div>
    There will most likely be conflicts. These can be avoided by formally declaring the variable as global, also as previously mentioned.

    Extra Stuff:

    One thing I didn't make clear though is, if you use the window.myVar2 method:

    Code:
    function myFunc(){
       var myVar = 500 /* local variable */
       window.myVar2 = 600 /* global variable */
    }
    Then myVar2 is available outside the function as myVar:

    Code:
    function myFunc(){
       var myVar = 500 /* local variable */
       window.myVar2 = 600 /* global variable */
    }
    myFunc();
    alert(myVar2); /* alerts 600 */
    But, as I also mentioned, declaring it before hand is preferable:

    Code:
    var myVar2;
    function myFunc(){
       var myVar = 500 /* local variable */
       myVar2 = 600 /* global variable */
    }
    myFunc();
    alert(myVar2); /* alerts 600 */
    That also avoids a conflict with the document object, and avoids an error in cases like so:

    Code:
    var myVar2;
    function myFunc(){
       var myVar = 500 /* local variable */
       myVar2 = 600 /* global variable */
    }
    alert(myVar2); /* alerts 'undefined' */
    Whereas with the window.myVar2 approach:

    Code:
    function myFunc(){
       var myVar = 500 /* local variable */
       window.myVar2 = 600 /* global variable */
    }
    alert(myVar2); /* throws an error */
    Extra Extra Stuff:

    All of this is interesting and can contribute to one's understanding in working with older code, and by extension to an understanding of scope in any code. For new code though, it's best to avoid situations like these in the first place by making all variables except possibly one local to some scope:

    Code:
    function SomeConstructor(params){
      whatever . . .
    }
    Invoked:

    Code:
    new SomeConstructor(params);
    The SomeConstructor function can be global, but all subsequent functions and values can be prototype functions and values of the constructor, or objects of it.

    If all interactive events are assigned by SomeConstructor and it's prototypes and objects - SomeConstructor, it's prototypes and objects may be declared within an anonymous function, exposing nothing to the global scope.
    Last edited by jscheuer1; 04-13-2011 at 07:28 AM. Reason: English Usage
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  7. The Following User Says Thank You to jscheuer1 For This Useful Post:

    traq (04-13-2011)

  8. #6
    Join Date
    Oct 2007
    Posts
    38
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    good informative post
    thanks very much for it.
    i actually got what i needed to now as soon as you said that the variable was undefined and could lead to conflicts with the rest of the script. that is what was happening.
    i used the approach where i defined the var outside the function, and the code works now.
    thanks again
    best regards

Bookmarks

Posting Permissions

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