`import` and aliasing bindings

Andrea Giammarchi andrea.giammarchi at gmail.com
Fri Dec 28 09:53:46 PST 2012


why poorly supported? that logic is easy to reproduce in ES5 modules, isn't
it?

// a.js
var B;
function A() {}

// runtime each time
A.prototype.m = function () {
  return B ? new B : (B = require('B'), new B);
};

// lazily optimized
A.prototype.m = function () {
  B = require('b');
  return (A.prototype.m = function () {
    return new B;
  })();
};

exports.A = A;

I think cross dependency should rarely be an advantage since I find it most
of the time an architecture problem ... not sure is a good thing that's
simplified, I like the lazy require possibility in any case for all other
things such require what you need when you need and not always regardless.
This is good practice in node.js modules too.

br



On Fri, Dec 28, 2012 at 6:43 PM, David Herman <dherman at mozilla.com> wrote:

> On Dec 28, 2012, at 9:39 AM, Domenic Denicola <domenic at domenicdenicola.com>
> wrote:
>
> > On Dec 28, 2012, at 12:28, "David Herman" <dherman at mozilla.com> wrote:
> >
> >> Another example where this could come up is initialization. Since
> imports tend to be at the beginning of a module, you could end up reading
> the value of a not-yet-initialized variable too soon.
> >
> > A code example of this would be awesome. Also, is it a problem ES5
> module systems fail to address?
>
>
> It can come up with mutual recursion between modules, which ES5 module
> systems famously handle poorly. Here's an example, untested, obviously :)
>
> // a.js
> import { B } from "b";
>
> export class A {
>     m() { return new B }
> }
>
> // b.js
> import { A } from "a";
>
> export class B {
>     m() { return new A }
> }
>
> Dave
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20121228/6d568a80/attachment.html>


More information about the es-discuss mailing list