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 12 of 12
  1. #1
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Unhappy Javascript Global Variable Calling, need help

    <script>
    var global = 10;

    function global(){
    var local = 5;
    console.log(local); // output: 5
    console.log(global); // output: returns function body
    }
    global();
    </script>

    as you can see, both global variable and global function have the same name 'global'.
    my question is, how can i call global variable from inside the global function.

    please answer as soon as possible. thanks in advance.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    You cannot have a function and a variable both with the same name. Simple answer - rename your variable.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Philip M View Post
    You cannot have a function and a variable both with the same name. Simple answer - rename your variable.
    can you please explain more? because dreamweaver and firebug both show no syntax error.
    so that means a function and variable can have same name in same scope. right?

    i know that in a real project, i wont be naming functions and variables with a same identifier but what to do in this case?
    i am a beginner and javascript is confusing me a lot.

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    You cannot have a function and a variable both with the same name
    and the same scope. The code below takes the function out of the
    global scope.
    Code:
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title></title>
    </head>
    <body>
    <script>
     var global = 10;
    ( function global(){
          var local = 5;
         alert(local); // output: 5
         alert(global); // output: returns function body
         alert(window.global);// output: returns 10
     })();
     </script>
    </body>
    </html>
    Last edited by DaveyErwin; 09-01-2014 at 09:33 PM.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    use window.global or self.global to reach the outside, provided that's the entire code and the outer one is truly global.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,080
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by inferno847 View Post
    can you please explain more? because dreamweaver and firebug both show no syntax error.
    so that means a function and variable can have same name in same scope. right?

    i know that in a real project, i wont be naming functions and variables with a same identifier but what to do in this case?
    i am a beginner and javascript is confusing me a lot.

    The error is not a syntax error.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    inferno847 (09-01-2014)

  • #7
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by rnd me View Post
    use window.global or self.global to reach the outside, provided that's the entire code and the outer one is truly global.

    already tried. but output the whole function body.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    In your original code
    Code:
    <script>
    var global = 10;
    
    function global(){
        var local = 5;
        console.log(local); // output: 5
        console.log(global); // output: returns function body
    }
    global();
    </script>
    when executed, the line in red gives the error (in Chrome) of
    uncaught Type error: number is not a function
    and MSIE gives an error of
    Function expected
    In other words, the first occurrence of global is used, so essentially global() is trying to do 10()

    I don't know what environment you are testing your code in, but if it is DreamWeaver, then you should ignore anything DrunkWalker tells you. It's correct name is NightmareMaker. Your code should not run; it should never gt to the point where it outputs the function body.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by Old Pedant View Post
    In other words, the first occurrence of global is used
    only by the looks. it actually uses the last assignment to global which is, thanks to function hoisting, the number assignment.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Oh, right you are. I forgot about function hoisting. Thanks.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    New to the CF scene
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    and now i will be using aptana studio as my new editor. thanx man. dreamweaver sux. it really didnt show any error no one should waste money on it.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,198
    Thanks
    80
    Thanked 4,453 Times in 4,418 Posts
    Once again, you did *NOT* have a SYNTAX error. And EDITORS will typically only detect syntax errors and leave the discovery of run-time errors to the browser. Using a full blown IDE (which I presume is what Aptana is) you will typically have a browser (or at least a JS "engine") as part of the IDE. Different kettle of flounder.

    I don't like DoffusWhacker but AS AN EDITOR it is not that bad.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

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