<div dir="ltr"><font style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px">Is it possible that Crock meant export declarations should be restricted to the *top level* of the file? In other words, they can't be nested in conditional blocks or inside functions, but they can appear anywhere in the outermost scope of the module?</font><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2">If so, that's effectively what ECMAScript 2015 already requires: <a href="http://www.ecma-international.org/ecma-262/6.0/index.html#sec-modules">http://www.ecma-international.org/ecma-262/6.0/index.html#sec-modules</a> (note that<span class="inbox-inbox-Apple-converted-space"> </span><i>StatementListItem</i><span class="inbox-inbox-Apple-converted-space"> </span>can never produce additional<i>ExportDeclaration</i><span class="inbox-inbox-Apple-converted-space"> </span>symbols).</font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2">All export declarations define some number of exported symbol names (even<span class="inbox-inbox-Apple-converted-space"> </span><font face="monospace">export default</font>declarations, where the name is "default"). Some export declarations also declare local variables, functions, or classes, or evaluate default expressions. It's important to keep in mind that the definition of exported symbols is conceptually hoisted to the beginning of the module, while the initialization of local entities happens wherever it normally would (function declarations are also hoisted, but variables and classes are not, and of course default expressions must be evaluated in their original position, for the reasons mentioned by Logan S.).</font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2">In other words, for export declarations that<span class="inbox-inbox-Apple-converted-space"> </span><i>only</i><span class="inbox-inbox-Apple-converted-space"> </span>define exported symbols (e.g.<span class="inbox-inbox-Apple-converted-space"> </span><font face="monospace">export { a, b as c }</font>), it doesn't really matter where they appear in the file, because (thanks to hoisting) it's as if you wrote them at the beginning of the file, anyway.</font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2">That's my understanding, at least :)</font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif;font-size:13px"><font size="2"><br></font></div><div style="color:rgb(33,33,33);font-family:"helvetica neue",helvetica,arial,sans-serif">Ben</div><br><div class="gmail_quote"><div dir="ltr">On Tue, Jun 7, 2016 at 12:58 AM Logan Smyth <<a href="mailto:loganfsmyth@gmail.com">loganfsmyth@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div style="font-size:12.8px"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote">```js<br><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">export default myConst;<br></span><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">const myConst = {};<br></span>```<br style="background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">would throw because when the export is evaluated</span></blockquote><div><br></div></div></div><div dir="ltr"><div style="font-size:12.8px"><div>Correct, if you wanted to export `myConst` as the default without that issue, you'd want to do</div><div><br></div><div>```</div><div>export {myConst as default};<br>const myConst = {};<br>```</div><div>to declaratively expose the binding under the default name, without requiring the value be available at the time.</div><div><br></div><div><div class="gmail_extra" style="font-size:small">The key thing to remember with exports is that they are all processed before the JS has even begin executing, and as you saw, are no-ops at execution time, aside from `export default myConst` which is essentially sugar for</div><div class="gmail_extra" style="font-size:small"><br></div><div class="gmail_extra" style="font-size:small">```</div><div class="gmail_extra" style="font-size:small">const _hiddenBinding = myConst;<br>export {_hiddenBinding as default};<br>```</div></div><div class="gmail_extra" style="font-size:small"><br>where the fact that a default binding exists is known at parse time. Only the value of the export is assigned at evaluation time.<br></div></div></div><div dir="ltr"><div style="font-size:12.8px"><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span style="font-size:12.8px">```js<br></span><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">export {myConst};<br></span><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">const myConst = {};<br></span><span style="font-size:12.8px">```</span></blockquote></div></div><div dir="ltr"><div style="font-size:12.8px"></div><div class="gmail_extra"><br></div><div class="gmail_extra">This is not an issue. Export declarations define mappings of module-local binding names, to publicly exposed export names, and that is all, they do not access the value in any way on their own. It's possible that if you had some circular dependencies in your code, something could access `myConst` before it had been initialized, and that would result in a TDZ error the same way any other attempt to access the value would.</div></div><div dir="ltr"><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra"><div class="gmail_quote">On Mon, Jun 6, 2016 at 9:39 PM, Raul-Sebastian Mihăilă <span dir="ltr"><<a href="mailto:raul.mihaila@gmail.com" target="_blank">raul.mihaila@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Douglas Crockford said that eventually JSLint would require exports at the top of the file. However I think there are some issues.<div><br></div><div><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">If my understanding is correct,</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><br>```js<br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">export default myConst;</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">const myConst = {};</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">```</div><div><br style="background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">would throw because when the export is evaluated, in step 2 of the Evaluation algorithm applied to the ExportDeclaration: export default AssignmentExpression; production (</span><a rel="nofollow" href="https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation" dir="ltr" style="text-decoration:none;color:rgb(41,98,255);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)" target="_blank">https://tc39.github.io/ecma262/#sec-exports-runtime-semantics-evaluation</a><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">), the value of the assignment expression is retrieved, which should cause an error because the binding has not yet been initialized.</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">I'm not sure what should happen if myConst was exported with an ExportClause.</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><br>```js<br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">export {myConst};</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">const myConst = {};</span><br style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">```</div><div><br style="background-color:rgb(243,243,243)"><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">In this case, the Evaluation algorithm just returns a normal completion. But I think that it depends on when myConst is indirectly accessed in the importing modules. If it's accessed before `const myConst` is evaluated, then I believe it would be uninitialized and would throw. Otherwise, it would work.</span><br></div><div><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)"><br></span></div><div><span style="color:rgba(0,0,0,0.870588);font-family:Roboto,RobotoDraft,Helvetica,Arial,sans-serif;font-size:14px;line-height:20px;white-space:pre-wrap;background-color:rgb(243,243,243)">Is my understanding correct? Thanks!</span></div></div>
<br>_______________________________________________<br>
es-discuss mailing list<br>
<a href="mailto:es-discuss@mozilla.org" target="_blank">es-discuss@mozilla.org</a><br>
<a href="https://mail.mozilla.org/listinfo/es-discuss" rel="noreferrer" target="_blank">https://mail.mozilla.org/listinfo/es-discuss</a><br>
<br></blockquote></div><br></div></div>
_______________________________________________<br>
es-discuss mailing list<br>
<a href="mailto:es-discuss@mozilla.org" target="_blank">es-discuss@mozilla.org</a><br>
<a href="https://mail.mozilla.org/listinfo/es-discuss" rel="noreferrer" target="_blank">https://mail.mozilla.org/listinfo/es-discuss</a><br>
</blockquote></div></div>