Javascript: How to access an imported module variable from inside another imported module?

Posted in :

<script> 目前有分二種,之前的<script> 與目前新的 <script type=”module”>, 同一個 module 裡的變數,在這一個 module 之外是看不到的。

我遇到的問題是,在修改別人提供的範例時,在範例裡使用了 module 來處理網頁內容,遇到需要跨 module 時,別的script 無法存取 module 裡的內容。

Module resolution was implemented exactly to don’t pullulate the global scope and avoid the programmers ask “does this variable exist yet?”.

我用的是這篇裡的說明,把內容放到 window.your_object 就完成全域處理。

Modules, introduction
https://javascript.info/modules-intro

這個作者,另外寫的2篇,我覺得參考性佳,很適合完全不懂 module 的我來看,寫的很易懂。


相關討論:

How to access an imported module variable from inside another imported module?
https://stackoverflow.com/questions/64621340/javascript-how-to-access-an-imported-module-variable-from-inside-another-import

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *