<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>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 class="">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><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 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 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>