`import` and hoisting

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

`import` and hoisting

Axel Rauschmayer
As far as I can tell, `import` is hoisted (due to `ModuleDeclarationInstantiation`). Is the following code OK, then? No temporal dead zone?

```js
bar();

import {foo} from 'mymodule';

function bar() { // hoisted!
    foo(); // already initialized?
}
```

How about this code?

```js
foo();

import {foo} from 'mymodule';
```

Thanks!

Axel

-- 
Dr. Axel Rauschmayer
[hidden email]
rauschma.de




_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: `import` and hoisting

Kyle Simpson
bump.
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: `import` and hoisting

Caridy Patino
In reply to this post by Axel Rauschmayer
yes, they will work. "hoisting of function initialization happens across the whole module linkage graph before any module initialization code starts executing." quoting @dherman.

there is one thing to keep in mind though: many edge cases cannot be transpile into CJS, AMD, etc., the only format that preserves all the semantics of the module system is the `system` format, which is an experimental format we came up with a while ago. those two examples might fail in babeljs, but they definitely work on esnext module transpiler (which is already deprecated), but at least will give you an idea of how things work internally: http://bit.ly/1ETGVDA

/caridy

> On Mar 20, 2015, at 2:20 PM, Axel Rauschmayer <[hidden email]> wrote:
>
> As far as I can tell, `import` is hoisted (due to `ModuleDeclarationInstantiation`). Is the following code OK, then? No temporal dead zone?
>
> ```js
> bar();
>
> import {foo} from 'mymodule';
>
> function bar() { // hoisted!
>     foo(); // already initialized?
> }
> ```
>
> How about this code?
>
> ```js
> foo();
>
> import {foo} from 'mymodule';
> ```
>
> Thanks!
>
> Axel
>
> --
> Dr. Axel Rauschmayer
> [hidden email]
> rauschma.de
>
>
>
> _______________________________________________
> es-discuss mailing list
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss