1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Javascript Scoping & Hoisting

Discussion in 'JavaScript' started by serialentre, Apr 20, 2015.

  1. #1
    Hi all,

    I am trying to understand what Javascript Scoping & Hoisting is. Chanced upon this article during my research.

    http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

    Don't quite understand what Scoping & Hoisting is about. Appreciate any help. Thanks!
     
    Solved! View solution.
    serialentre, Apr 20, 2015 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #2
    I've never seen anyone apply "ing" to scope in reference to JS before, nor have I seen anyone call simple order of execution "hoisting".

    Scope is scope, anything declared with VAR only exists inside the scope it is declared -- like a function or object method. The whole "hoisting" nonsense shouldn't even have an impact since the variable being declared shouldn't matter, since you wouldn't be accessing it outside the scope anyways.

    Looks to me like yet another example of someone taking something simple like scope, and trying to explain how it works in the most complicated manner possible.
     
    deathshadow, Apr 20, 2015 IP
  3. serialentre

    serialentre Member

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    30
    #3
    Hi Deathshadow,

    Thanks for your reply. In this case, it's basically the concept of Scope is that correct?

     
    serialentre, Apr 20, 2015 IP
  4. #4
    Yeah, overall the concepts presented are correct, just poorly worded and throwing more terms at it than needed.

    What they call "hoisting" is just that all variables declared in a function scope are done before the function executes even if they aren't used, so the stack can be allocated properly. (this is why some languages like pascal and modula make you declare all variables BEFORE you open the code block of a function or method). C does this at the code block open/close level, while JavaScript only does it at the function level.

    ... as such JS scope is function-wide, while C scope is block-wide after any evaluation.

    The article makes a bit more sense if you understand C, but if you don't already know C and how it's compiled, it's going to confuse the crap out of people who only know JS. It's why I think more programmers need to learn the underlying machine language concepts BEFORE diving into high level languages, so that things like the stack, heap and general memory allocation are at least vaguely understood. High level languages are nice that they take a lot of the grunt-work out of it, but they often hide the underlying mechanisms from developers preventing learning how things "really" work. Vaguaries like this "scoping" and "hoisting" are a result.

    ... as are a LOT of the memory and CPU chewing oddities I've been seeing from a lot of JS, PHP, JAVA and C# developers of late; things that seem to be becoming common practice that older dev's like myself look at and go "REALLY? REALLY?!? really..." :(
     
    deathshadow, Apr 20, 2015 IP
  5. serialentre

    serialentre Member

    Messages:
    123
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    30
    #5
    Thanks Deathshadow!

    Have been learning a little bit of C but unless I get my hands dirty, they are all just concepts in my head!
     
    serialentre, Apr 22, 2015 IP