在我提供我的 JavaScript 文件之前type="module",我可以在我用來返回它們的同一語句中定義我的變量。例子:function formatNameForFile(name) { return formattedName = name.toLowerCase().replace(/ /g, '-');}然而,一旦我添加type="module"到我的 JavaScript 文件中,我收到以下引用錯誤:Uncaught ReferenceError: formattedName is not defined我可以通過在返回變量之前明確定義我的變量來消除這些錯誤,如下所示:function formatNameForFile(name) { const formattedName = name.toLowerCase().replace(/ /g, '-'); return formattedName;}為什么是這樣?這些變量是否仍然可以通過初始 return 語句訪問,只要它們在其中綁定(使用const、var或)?let
ES6 模塊:必須在返回變量之前顯式定義變量嗎?
慕妹3242003
2023-03-24 16:22:01