Modules vs Scripts

David Herman dherman at mozilla.com
Sat Nov 16 00:30:30 PST 2013


On Nov 16, 2013, at 3:32 AM, John Barton <johnjbarton at google.com> wrote:

> Could someone help me understand why two goals for parsing JS is a good thing?

Hm, it sounds like you've assumed a conclusion already. Let me try to explain anyway.

Scripts are for synchronous loading and evaluation; modules are for asynchronous loading and evaluation. The former is not allowed to import, which triggers I/O. The latter is allowed to import. There was always going to have to be a syntactic split.

But there's more. I'll be explaining at this week's TC39 meeting some more details about the intended model for browser integration. In short, declarative code based on modules will use the <module> tag (or <script type="module">, which will mean the same thing and allows polyfilling today). This is better than <script async>, which was a hopeless idea. This also works beautifully for fulfilling the promise of 1JS: a new, better initial environment in which to run top-level application code without requiring versioning.

    <module>
      var x = 12;             // local to this module, not exported on global object
      import $ from "jquery"; // asynchronous dependency, NBD
      '$' in this             // nope, because all globals are scoped to this module
      let y = 13;             // yep, let works great because modules are strict
    </module>

Same number of characters as <script>, better semantics, better global environment, better language.

If you're not excited about ES6 yet, get excited. :)

Dave



More information about the es-discuss mailing list