JavaScript for WordPress Forums Gutenberg Development Preventing global scope and babel

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
  • #39481

    I have a question about the lesson “The Modern JS vs ES5 Approach”. Could you tell us a little more about the global scope? I know that it’s enough to make {const} in es6, but the babel will turn it into {var} and the variable will leak out of scope. So I’d better use the old method (function(){const})()?

    Zac Gordon

    Hi there! Sorry for the delay on this. I wanted to first release my Vanilla JS Course, which delves deeply into state before I responded so if you wanted to dig deeper, resources were available to do so 🙂

    But in short, global scope is the scope stored in the window object and available to all JS files that load on the same page.

    When you write const and use imports, webpack will wrap the variables in global scope to prevent them from leaking into the global scope 🙂

    Also, as browsers evolve, the conversion of const to var can become less needed.

    Hope that helps!

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.