在身体中未识别的头部定义的功能?

发布于 2025-02-07 06:47:25 字数 260 浏览 4 评论 0原文

大家

<script type="module">
import {g1} From './scripts/main.js';

myFunction(){
do something, g1 is called
}
</script>

<script>
myFunction();
</script>

​与头部类型属性的错误?

Hi all I'm learning javascript and have run into a problem when defining a function in head utilizing imported functions:

<script type="module">
import {g1} From './scripts/main.js';

myFunction(){
do something, g1 is called
}
</script>

when I call myFunction in body I get a myFunction() not defined error:

<script>
myFunction();
</script>

My understanding is this should work, am I making a mistake with the type attribute in head?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唯憾梦倾城 2025-02-14 06:47:27

在JS中,给定代码的范围仅像这样执行:

(1)全局范围&gt; (2)模块范围,然后&gt; (3)功能范围

意味着您尝试访问全局范围中的模块范围变量/函数,您需要在某个地方进行编码。您的选项是:

  • 要么将代码放在同一模块中
  • ,要么找到将一个模块导入另一个模块的方法

In JS the scope of a given code is executed simply like so:

(1) Global scope > (2) Module scope and then the > (3) Function scope

Meaning that you try to access a module scope variable/function in the global scope, you need to restructure you code someway. Your options are:

  • Either to put the code within the same module
  • Or find a way to import one module into the other one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文