News for hackers. Stuff that matters.
Re:The Future of JavaScript by substack on Friday February 10, 2012 @07:47PM (#3578159)
I really dislike what they're doing with modules because it's such a step back from the commonjs-inspired require() that node uses. In node, require() just returns a value. That value is usually an object but often it's just a function. Why should a module that encapsulates exactly one function return anything else but that one function?

In ES.next, you've got to use pythonesque `import y from Bar` statements which introduce a frustrating correspondence between the lexicals of your own program and the export names of the module you're trying to use which is really terrible if you're using as many modules as I typically do. I presume ES.next will invent even more specific syntax for some sort of `as` like keyword to side-step this but it seems so unnecessary when javascript already has a module system that is so very good as is exploding in popularity and use (>7000 modules now on http://search.npmjs.org/).

I understand what they're trying to do with static analysis too but you can already pretty much do that and I've done it. It's not hard at all, just ignore require() statements that don't contain strings when you walk the AST, like this: https://github.com/substack/node-detective

Inventing more syntax instead of just adopting a far better module system that has seen actual use in practical situations is such a shame and ES.next is rife with this kind of prescriptivism. It irritates me to no end and I sometimes want ES.next to die a quiet death in obscurity because of it.

http://wiki.ecmascript.org/doku.php?id=harmony:modules

Re:The Future of JavaScript by ootachi on Saturday February 11, 2012 @01:13AM (#3578711)
You haven't considered optimization -- with closed modules, the compiler can statically know, for instance, that "Math.sin" is actually the sine function and can compile that straight down to the hardware instruction. No speculation or guards are necessary.

Additionally, your static analysis must be unsound if modules are mutable. The problem is not figuring out which module is being imported, it's figuring out whether the bindings are mutated. CommonJS cannot guarantee that modules haven't been mutated.

And yes, you can rename imports. Why is "var { foo: bar } = require('baz')" so much better than "import { foo: bar } from baz;"? They look the same to me, except that the ES6 one is better for static analysis and better for optimization.

Re:The Future of JavaScript by RobertWHurst on Monday February 20, 2012 @11:11PM (#3615186)
I don't think you get it. The right way to require a module in commonjs is var baz = require('baz'). The reason its better is because I can accomplish just as much as the ridiculous ES6 modules and its simple. We don't need to add complexity if it doesn't get us anywhere. I'm not convinced that static analysis is worth it. Existing module systems work just fine without this shit.
Re:The Future of JavaScript by samth on Saturday February 11, 2012 @12:38PM (#3579652)
I'll try to separate out the different issues you're raising here, and respond to each individually.

1. What if I want to have my module be a function, the way that $ in jQuery is both a function and a namespace for the rest of the functionality?

This is an important use case, and so we're going to support it in ES6 modules. You'll be able to say:

    module $ at "http://jquery.com/jquery.min.js";
    $(...)
    $.ajax(...)
2. Modules will requiring using names without the prefix.

This isn't true. As you can see in the above example, you can just use $ directly, even though it's a module. You can also say:

   import ajax from $;
but you don't have to.

3. We should just standardize the node module system.

Of course, there are many different module systems being used in JS code right now, and we could just pick one. Unfortunately, we couldn't just pick the node module system, since it's fundamentally synchronous, which is great on the server side but not in the browser.

However, we feel that we can bring real advantages to JS programmers by extending the language -- we can simplify using modules, we can make some patterns direct that currently have to be written using callbacks, we can better support encapsulation, and we can allow engines to use knowledge about modules for optimization.

3. Static analysis is doable even without a static module system.

The static analysis you describe, while useful, isn't going to tell an engine statically where it can go to lookup references to an export from a module. That means it's not useful for many of the optimizations that we want to enable. Additionally, your analysis isn't sound -- it can miss uses of `require` that are generated dynamically, for example. Again, that means that engines can't use it for optimization.

Finally, if you have comments about ES development, I strongly encourage you to make them on es-discuss, rather than on HN, where they are more likely to have an impact on the development of the language.

Re:The Future of JavaScript by medikoo on Thursday February 16, 2012 @03:46AM (#3597889)
I'll just add that I totally I agree with samth

I'm strong user of Node.js style modules (also for client side) and I see modules as proposed for Harmony as big step forward. It's basically same concept but with dedicated syntax (well put syntax) and some extra powerful features.

I've once setup some comparison how today modules written for Node.js (like modules that are functions) will look in harmony. I didn't spot any issues. See slides 86-87 at http://www.slideshare.net/medikoo/javascript-modules-done-ri...

Re:The Future of JavaScript by samth on Thursday February 16, 2012 @08:04AM (#3598406)
Since it came up on twitter, here's how you'd implement the `$` module I describe above:

    export function ajax(...) { ... };
    
    // NB: not how jQuery actually works ;)
    export this(query) { return document.find(query); };
The `export this` syntax specifies how the module instance object behaves when called as a function.
Re:The Future of JavaScript by substack on Thursday February 16, 2012 @09:07AM (#3598616)
Thanks for this. I'm glad that using a single export function will at least be possible. I'm still pessimistic about the amount of syntax being introduced here.

In particular, I still really dislike the special import "local renaming" syntax since it seems so unnecessary compared to just having an import keyword that returns an object and letting the programmer extract and manipulate the relevant entries. You would still get the static analysis benefits of having static imports without all the awkward complexity of the current proposal. Wouldn't it just be the same thing to Object.freeze() the export object but without any syntax magic necessary?

Plus, overloading `module` to do loading AND defining with the `module Bar at "uri"` form seems really wrong. Why can't it JUST do defining and just let import do all the loading?

Re:The Future of JavaScript by samth on Thursday February 16, 2012 @09:27AM (#3598735)
First, if you want to just use objects-as-prefixes, the way require(...) works in node today, that's easy to do:

    module m at "blah.com";
    ... references to m go here ...
We also want to support other use cases, like binding the exports to names available in your local scope. We understand that not everyone likes that style, but I don't think the language should mandate a particular side in that fight.

Second, the `module m at "URL"` form above is a definition of `m`. It's just that you're allowed to refer to that binding as an object too. Would you rather have:

    module m at "example.com";
    import m;
That just seems like unnecessary typing to me.
Re:The Future of JavaScript by substack on Thursday February 16, 2012 @10:03AM (#3598937)
I mean why does `module` pull down resources? Shouldn't that be import's job?
Re:The Future of JavaScript by samth on Thursday February 16, 2012 @10:39AM (#3599102)
There are two reasons:

1. `module` is about defining the modules that you're using in your program, some of which might be remote resources. `import` is about bringing names into scope.

2. Using `module` this way lets you, the client, decide on names for modules, which means we don't have to rely on module authors to come up with and use naming conventions (net.substack.airport, anyone?).

This feeds back into 1, where we use bindings to reference modules, even remote ones.

Also, if a module is never referenced or imported, then the resources shouldn't need to be pulled down at all. So partly it depends on the way you look at things.

Re:The Future of JavaScript by samth on Saturday February 11, 2012 @12:44PM (#3579661)
To make my perspective here clear, I should mention that Dave Herman (of Mozilla Research) and I are the designers of the ES6 module system.
Re:The Future of JavaScript by erichocean on Saturday February 11, 2012 @12:09AM (#3578627)
Totalyl agree. The lack of a module system in JavaScript has been an enormous benefit to the developer community, because it has allowed us to develop idioms that make sense in the context of JavaScript.

Adopting a hard-coded, python-esque module system is a step backwards from what we have now, and I doubt will be used much, if at all -- why give up features that already exist (and will continue to work)?

sigh

Re:The Future of JavaScript by batista on Saturday February 11, 2012 @05:31PM (#3580375)
Sure, the top JS language designers in the ES6 group haven't thought of these issues...
Re:The Future of JavaScript by mcantelon on Friday February 10, 2012 @08:30PM (#3578260)
Attempts to create big iterations on successful languages seem to consistently fail to win adoption.
Re:The Future of JavaScript by gjm11 on Friday February 10, 2012 @09:14PM (#3578365)
C++ seems like a pretty obvious counterexample, even though it didn't call itself "C". Others: Fortran 90. Common Lisp. Visual Basic. [EDITED to add: Perl 5.]

ANSI C was quite a big change from K&R, even.

Re:The Future of JavaScript by batista on Saturday February 11, 2012 @05:33PM (#3580378)
Not to mention: C11x (or whatever it's called), which has huge support from vendors and C++ guys, C# 4.0 that is miles ahead of C# 1.0,
Re:The Future of JavaScript by Andi on Friday February 10, 2012 @10:53PM (#3578527)
+1 (and substack is talking)