在另一个 .js 文件中调用 javascript 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5785913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:50:04  来源:igfitidea点击:

call a javascript method in another .js file

javascriptjquery

提问by leora

what is the rule around calling functions from one js to another ? I thought that this worked but i am not running into issue (figured it out through firefox) that a function in another js file doesn't seem to be recognized in my first js file.

从一个 js 调用函数到另一个 js 的规则是什么?我认为这有效,但我没有遇到问题(通过 firefox 解决)另一个 js 文件中的函数在我的第一个 js 文件中似乎无法识别。

Is there some rule around ordering or some trick you have to do to get this to work?

是否有一些关于订购的规则或一些你必须做的技巧才能让它发挥作用?

采纳答案by Austin Taylor

Are you calling the function in an event handler, or immediately when the javascript file is loaded? If it's not in an event handler, then load order is important. If you have circular dependencies, you may need to delay some of the initialization with a "DOM ready" or window.onLoadlistener.

您是在事件处理程序中调用该函数,还是在加载 javascript 文件时立即调用该函数?如果它不在事件处理程序中,则加载顺序很重要。如果您有循环依赖,您可能需要使用“DOM 就绪”或window.onLoad侦听器来延迟一些初始化。

回答by icktoofay

It has to be accessible in the global scope somewhere. For example:

它必须可以在全局范围内的某个地方访问。例如:

// file1.js
function hello() {
    alert("Hello, world!");
}
// file2.js
$(function() {
    hello();
});

Likely, you have something like this:

可能,你有这样的事情:

// file1.js
$(function() {
    function hello() {
        alert("Hello, world!");
    }
    // ...
});
// file2.js
$(function() {
    hello();
});

hellois only in scope of the closure defined in file1.js. Therefore, to access it in file2.js, you'd have to export it to somewhere where file2.jscan get at it:

hello仅在 中定义的闭包范围内file1.js。因此,要访问它file2.js,您必须将其导出到file2.js可以获取它的地方:

// file1.js
$(function() {
    function hello() {
        alert("Hello, world!");
    }
    window.hello=hello;
});
// file2.js
$(function() {
    hello();
});

Also, the script where the function is defined must be loaded, parsed, and executed before the function can be called from another script.

此外,必须先加载、解析和执行定义函数的脚本,然后才能从另一个脚本调用该函数。

回答by Kyle Rogers

The browser parses the javascript files in the order they appear in the HTML. So if a function that is excecuted in the first file relies on a function that is in the second file it won't work. If you use $(function() {}); for example with jQuery this is instructing the javascript to wait until the onload event is fired from the window object. This ensures all the elements on the page have been loaded prior to execution.

浏览器按照它们在 HTML 中出现的顺序解析 javascript 文件。因此,如果在第一个文件中执行的函数依赖于第二个文件中的函数,它将无法工作。如果你使用 $(function() {}); 例如,对于 jQuery,这是指示 javascript 等待,直到从 window 对象触发 onload 事件。这可确保页面上的所有元素在执行之前都已加载。