Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 调用函数会导致引用错误,因为函数未定义_Javascript - Fatal编程技术网

Javascript 调用函数会导致引用错误,因为函数未定义

Javascript 调用函数会导致引用错误,因为函数未定义,javascript,Javascript,我有两个JS文件-first.JS和second.JS 我已经在second.js中直接定义了一个函数call(),而没有将其包含在任何内容下 #second.js function call(){ console.log('output'); } 现在,当我从first.js调用它时,它给出了引用错误,因为函数call()未定义 #first.js function test(){ call(); } 我在main.html中按以下顺序调用这两个单独的js文件

我有两个JS文件-
first.JS
second.JS

我已经在
second.js
中直接定义了一个函数
call()
,而没有将其包含在任何内容下

#second.js

function call(){
        console.log('output');
}
现在,当我从first.js调用它时,它给出了引用错误,因为函数
call()
未定义

#first.js

 function test(){
  call();
 }
我在main.html中按以下顺序调用这两个单独的js文件

<script type = "text/javascript" src="second.js"> </script>

<script type = "text/javascript" src="first.js"> </script>

既然在第一个函数之前包含第二个函数,那么函数
call()
已经存在,也没有包装在任何其他函数中,为什么会出现引用错误


EDIT:-根据建议,我检查了具有导致问题的onclick函数的
second.js
element.onclick=function(){$.get({{url\u for('python\u function')}};}
。我把它改为
element.onclick=function(){$.get({{url\u for(python\u function)}}})}
,现在一切都好了

代码中有一些错误。首先,您必须
second.js
中的代码导入
First.js
。看起来是这样的:

#first.js

import <name variable whatever you want> from '<path to second.js>';

 function test(){
  <name of the variable you made in the first line>();
 }

test();
<script type = "text/javascript" src="second.js"> </script>

<script type = "module" src="first.js"> </script>
最后,在HTML中,您希望将
first.js
的类型指定为
模块,而不是
text/javascript
,如下所示:

#first.js

import <name variable whatever you want> from '<path to second.js>';

 function test(){
  <name of the variable you made in the first line>();
 }

test();
<script type = "text/javascript" src="second.js"> </script>

<script type = "module" src="first.js"> </script>

希望这有帮助!让我知道它是否适合您

您确定第二个
加载正确吗?没有语法错误或什么?不需要,先生,我可以在控制台下看到它是唯一的错误,没有别的。我无法复制它,一定有一些代码或上下文缺失。它拼写错误,或者你真的将这些文件包含在
main.JS
文件中?如果键入
call(),会发生什么在控制台中?它运行吗?如果您正在使用模块,则无需包含
second.js
。@connexo您是对的!我将更新我的答案以反映这一点。谢谢您的帮助,先生,非常感谢!我已经做了编辑,请看你是否有时间。