Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

Understanding Function versus Global Scope - 10

Download

Right click “Save as…”

One often misunderstood topic that leads to errors in programs is how JavaScript treats variables declared inside and outside of functions. This video explains the ramifications of variable scope at both the functional and global levels. For developers coming from other programming languages, it demonstrates how your familiarity with block scope will lead you to erroneous conclusions about JavaScript's functional and global scope. Also briefly discussed is the notion of hoisting in JavaScript.

Tag:

Follow the Discussion

  • You Say about the myScope variable that you defined outside the
    anonymous function it's the same variable that you defined inside anonymous
    function and the value of variable is temporary change.
    but this is not true
    because the two variable have the different memory location not the same
    because in anonymous function you add the keyword var before the myScope
    variable.
    and if you want to be the two become the same memory location you need to
    remove the keyword var in anonymous function.

  • I would have expected in the 2nd alert box to see that myScope was changed to "changed", but it remains "global".

    myScope = "global";
    
    (function () {
        myScope = "changed";
        var myScope = "function";
        alert('inside the function, the scope is: ' + myScope);
    })();
    
    alert('outside the function, the scope is: ' + myScope);

  • tssuthatssutha

    @asok14215
    Please listen to him @7.20 to 7.40, he explain the reason for your output.

  • CindySCindyS

    Bob, I am enjoying these videos and learning much from them. Thank you for producing them!

    However, in this video I must say I was left somewhat confused. That is, I thought there should have been more in-depth explanation for the various cases and why the results are what they are.

    I too am confused by the results of the code posted by @asok14215. And while it MAY be true (don't know) that hoisting is responsible for the results (as suggested by @tssutha), I didn't get enough from this video to understand IF it's due to hoisting or WHY (if it even is). The example in the video @ 7:20 (ish) does not seem to me to be the same thing @asok14215 is doing.

    Can you offer any insight?

    Thanks again.

  • @CindyS: Hi Cindy ... check this out ... http://net.tutsplus.com/tutorials/javascript-ajax/quick-tip-javascript-hoisting-explained/ ... I think they do a nice job explaining it.  Also, did they explain it differently than I did?  I'm sorry to add confusion to the topic!

  • CindySCindyS

    Bob,
    Thanks for the very prompt response and link - I think I understand the hoisting concept now. You didn't add any confusion - I was already there... Don't know that nettuts explained it differently than you did, just a bit more thoroughly perhaps? As a side note, that site has a TON of information and appears to be an excellent resource (that I will now have to figure out a way to devote some time to!).

    I THINK I now understand what is happening behind the scenes in asok14215's code sample...the declaration (not the assignment) portion of:
    var myScope = "function";
    is hoisted to the beginning of the function block, then its value is set to 'changed', then its value is set to 'function', then the 1st alert() is executed, thereby displaying 'function' as the value. After that, the function block ends so the second alert when executed displays the globally-scoped value for myScope, which is still 'global'.

    In any case, it seems that declaring multiple variables with the same name - even if JavaScript permits it - is not a good idea and not a 'best practice.'

    Looking forward to continuing through the tutorials, so thanks again!

  • CindySCindyS

    Just to clarify - I didn't mean to suggest that YOU were saying declaring variables with the same name IS a good idea...I was simply offering my opinion on the subject.

    I guess I just don't see a good reason for doing that...perhaps there are reasons why you might have to (that I just haven't come across yet). Along the same lines, it seems to me it would be clearer and less risky to just declare the variables at the beginning of the block in which they are used, thereby eliminating hoisting issues altogether.

  • Re-opening this closed thread ...

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.