Understanding Function versus Global Scope - 10

Play Understanding Function versus Global Scope - 10

The Discussion

  • User profile image

    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
    and if you want to be the two become the same memory location you need to
    remove the keyword var in anonymous function.

  • User profile image

    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);

  • User profile image

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

  • User profile image

    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.

  • User profile image

    @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!

  • User profile image

    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!

  • User profile image

    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.

  • User profile image

    Re-opening this closed thread ...

Add Your 2 Cents