SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Global Constant Variables?

    I need to have a constant variable, one that can't be changed. in my javascript scripts...

    Code:
    const USER_ID = 10;
    this works, but when I attempt to access the constant through a function, then the scope of the constant doesn't seem to extend to functions.

    How do I make the constant global to all functions?
    I can't believe I ate the whole thing

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    const only works in gecko based browsers. So you would get JavaScript errors in other browsers...

    Back to your problem, can you provide more code, the following works just fine in Firefox
    Code:
    const USER_ID = 10;
    
    
    function test() {
    	alert(USER_ID);
    }
    
    test();

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    const is a new addition, which only works in spidermonkey and rhino (Eg. on firefox) AFAIK. I wouldn't recommend using it.

  4. #4
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah I noticed how it doesn't work at all in IE6 or IE7. Therefore, there is absolutely no use for it.

    Is there any other way to create constants in Javascript?
    I can't believe I ate the whole thing

  5. #5
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No, not real constants. You can define variables in uppercase to show that it should be a constant, but it can still be modified.

  6. #6
    SitePoint Guru
    Join Date
    Apr 2007
    Posts
    813
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There might be a way to protect a variable from being modified using protected variable, as defined by javascript guru Douglas Crockford, but I am not sure how to create a constant that is global to all scopes.

  7. #7
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SimplyFu View Post
    There might be a way to protect a variable from being modified using protected variable, as defined by javascript guru Douglas Crockford, but I am not sure how to create a constant that is global to all scopes.
    It's not possible. A protected variable isn't globally available. That's the whole point of it.

    Javascript is highly dynamic -- constants doesn't really fit into the language as such. For example, you can redefine a function in runtime, unlike most other languages.

  8. #8
    SitePoint Zealot the DtTvB's Avatar
    Join Date
    Jul 2006
    Location
    Thailand
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would create an annonymous function.

    Code:
    (function() {
    
    var USER_ID = 10;
    
    function testAn() {
        alert (USER_ID);
    }
    
    alert (USER_ID);
    testAn ();
    
    })();
    
    // Anything outside the above block won't have access to USER_ID.

  9. #9
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How would having an anonymous function help? It would not be a global variable and you can still change the value, meaning its not a constant.

  10. #10
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria View Post
    How would having an anonymous function help? It would not be a global variable and you can still change the value, meaning its not a constant.
    Nope. The USER_ID variable can only be accessed within the anonymous function and the anonymous function cannot be accessed since it doesn't have any name...

    When a variable is defined with var it is local to its scope and global to any functions defined within its scope. So if it is defined within a function then it only works within that function, however, if another function is for some reason defined within the function, then the variable will be global to the newly created function.

    Which clearly explains how this method works...
    I can't believe I ate the whole thing

  11. #11
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is one way to implement a global "variable" that can't be changed:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Constant "var"</title>
    <script type='text/javascript'>
    
    var c1 = constVar('X');
    var c2 = constVar(4.12);
    
    window.onload = function()
    {
      alert('c1 = ' + c1() + '\n' + 'c2 = ' + c2());
    }
    
    function constVar(value)
    {
      return function() { return value; };
    }
    </script>
    </head>
    <body>
    
    </body>
    </html>
    I do realize that it is now not a "variable" but a function - nevertheless its value can not be changed.
    Last edited by MikeFoster; Apr 18, 2007 at 11:24. Reason: typo?

  12. #12
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matsko View Post
    Nope. The USER_ID variable can only be accessed within the anonymous function and the anonymous function cannot be accessed since it doesn't have any name...
    Yes, and but the point of having a global constants is that you can access it globally.
    Quote Originally Posted by matsko View Post
    When a variable is defined with var it is local to its scope and global to any functions defined within its scope. So if it is defined within a function then it only works within that function, however, if another function is for some reason defined within the function, then the variable will be global to the newly created function.

    Which clearly explains how this method works...
    Yes, anonymous functions are clear to me, everything defined there will local to that function. But again, what you have there is no global constant.

  13. #13
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    Here is one way to implement a global "variable" that can't be changed:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>Constant "var"</title>
    <script type='text/javascript'>
    
    var c1 = constVar('X');
    var c2 = constVar(4.12);
    
    window.onload = function()
    {
      alert('c1 = ' + c1() + '\n' + 'c2 = ' + c2());
    }
    
    function constVar(value)
    {
      return function() { return value; };
    }
    </script>
    </head>
    <body>
    
    </body>
    </html>
    I do realize that it is now not a "variable" but a function - nevertheless its value can not be changed.
    Interesting approach! It can be changed though
    Code:
    var c1 = constVar('X');
    var c2 = constVar(4.12);
    
    window.onload = function() {
      alert('c1 = ' + c1() + '\n' + 'c2 = ' + c2());
    }
    
    function constVar(value) {
      return function() { return value; };
    }
    
    c1 = constVar("new value");
    Interesting thread!


  14. #14
    I'll take mine raw silver trophy MikeFoster's Avatar
    Join Date
    Dec 2002
    Location
    Alabama, USA
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He he. You didn't change it - you made another one , but I'm just joking around. I do see your point. Like kyberfabrikken said earlier, it is just not possible to implement "const" with Javascript. I guess we'll just have to wait for everyone to start using spidermonkey and rhino

  15. #15
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MikeFoster View Post
    I guess we'll just have to wait for everyone to start using spidermonkey and rhino
    Ah... I also have such dreams


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
  •