简体   繁体   English

有关从其他文件调用函数的问题-Javascript

[英]Question about functions being called from other files - Javascript

I am looking for some help understanding something on a project I am working on. 我正在寻找一些帮助,以了解我正在从事的项目中的某些内容。 I have written some code that is functioning, though I am not sure why. 我已经写了一些可以正常运行的代码,尽管我不确定为什么。

In a Node.js server, in /public/js there are two scripts. 在Node.js服务器中,/ public / js中有两个脚本。 One ( file1.js ) has a function func() . 一个( file1.js )具有函数func() file2.js , in the same directory, calls func() successfully. 在同一目录中的file2.js成功调用func() There is no module.export ing or require ing anywhere, yet the two files work together. 没有module.exportrequire在任何地方,但两个文件一起工作。 They are both referenced in the index.ejs file, however. 但是,它们都在index.ejs文件中引用。 Is this where they become able to communicate? 这是他们能够交流的地方吗?

//file1.js
function func() {
    console.log("foo")
}
//file2.js

func()
//index.ejs
...
<script src="public/js/file1.js"></script>
<script src="public/js/file2.js"></script>
...

I've spent all day reading and cannot find anything on this topic. 我花了一整天的时间阅读,找不到关于此主题的任何内容。

Your question is about how JavaScript works in a browser. 您的问题是关于JavaScript在浏览器中的工作方式。

Node.js isn't relevant here. Node.js在这里不相关。 All it is doing is running an HTTP server program that gives static files to the browser. 它所做的只是运行一个HTTP服务器程序,该程序将静态文件提供给浏览器。

When you load a script into a browser using a script element (and don't use type="module" ), any variable in the outer most scope of the script file (eg which isn't let inside a block or var inside a function) becomes a global and is accessible to any other script loaded into the same HTML document that way. 当加载脚本到使用脚本元件的浏览器(和不使用type="module" )中的脚本文件的最外范围,任何变量(例如未let块或内var内的函数)变成全局变量,并且可以通过这种方式访问​​加载到同一HTML文档中的任何其他脚本。

Globals are messy and a good way for different bits of code to accidentally interfere with each other so modern JavaScript generally avoids using them. 全局变量是一团糟,并且是不同代码段意外相互干扰的一种好方法,因此现代JavaScript通常避免使用它们。 This style of JavaScript programming wasn't common when JS was first implemented in browsers: Hence the behaviour described above. 最初在浏览器中实现JS时,这种JavaScript编程风格并不常见:因此,存在上述行为。

This is why people started to use techniques like the revealing module pattern and why AMD and Node modules were designed before standard JavaScript modules were added to the specification. 这就是为什么人们开始使用诸如显示模块模式之技术原因,以及为什么在将标准JavaScript模块添加到规范之前设计AMDNode模块的原因。

You must understand how the global space behaves in Javascript. 您必须了解全局空间在Javascript中的行为。

This Code: 此代码:

<script src="public/js/file1.js"></script>
<script src="public/js/file2.js"></script>

Is the same as this: 与此相同:

<script>
    //file1.js
    function func() {
        console.log("foo");
    }

    func();
</script>

Because soon as file1.js is loaded, everything that is defined inside of it, becomes available anywhere in that page from where it was included. 因为一旦加载了file1.js ,在其中定义的所有内容都将在包含该文件的页面中的任何位置变为可用。

Since file2.js uses contents of file1.js , it will work because func is available to be used anywhere below file1.js inclusion. 由于file2.js使用file1.js内容,因此它将起作用,因为func可以在file1.js包含以下的任何地方使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM