Advanced Search

Results 1 to 5 of 5

Thread: Making a Global Variable from Inside a Function?

  1. #1
    Join Date
    May 2006
    Location
    Alaska
    Posts
    163
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Default Making a Global Variable from Inside a Function?

    I was just wondering if it was possible to make a global variable from inside a function. Like:

    function blah () {
    var lol = "bye";
    }

    But make it so then you could use it in some other function. Like maybe:

    function alerter () {
    alert(lol);
    }

    I'm still developing my style of coding and this would be useful to know how to do (if possible).

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by ??? View Post
    I was just wondering if it was possible to make a global variable from inside a function.
    A variable, in a literal sense (see your deletion thread), can only be defined using a var declaration, therefore the statement must occur outside any function. However, a property can be created at any time, provided you have a reference to the object.

    Code:
    var global = this;  // You can use window, but I prefer not to.
    
    function foo() {
        global.property = 'value';
    }
    If the variable is predictable, I would suggest that you just declare it and reserve the above for cases where run-time creation really are necessary - for example, when the property name is chosen dynamically. Using a declaration makes it clear what variables exist, rather than forcing one to look through every line of code for an assignment.
    Mike

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    It's also usually a sign of bad code that it uses global variables. If you really must have global variables, you should create a single namespacing object and keep all the variables within that. If you find yourself using global variables a lot, then chances are there's a better way to accomplish whatever you're doing.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by Twey View Post
    If you really must have global variables, you should create a single namespacing object and keep all the variables within that.
    I don't really like "namespaces" in scripts. It's cumbersome and adds a performance penalty. Using a function expression called immediately avoids creating global variables, with only a one-off penalty rather than a cumulative one:

    Code:
    (function () {
        var sortOfGlobal;
    
        /* ... */
    })();
    Mike

  5. #5
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't really like "namespaces" in scripts. It's cumbersome and adds a performance penalty.
    A matter of taste, I think. Personally, I'd take the small performance penalty in exchange for increasing the modularity of my script, especially if it were a large one.

    The function expression does help avoid conflicts, but there are other benefits to namespacing.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •