javascript 在另一个 JS 文件中调用同名函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14765571/
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-10-26 22:31:31  来源:igfitidea点击:

Calling a function with same name in another JS file

javascriptfunctionvariablescount

提问by ninpot18

I'm just a bit confused here... If I have one .jsfile with function like this:

我在这里有点困惑......如果我有一个.js具有这样功能的文件:

function myMain() {
    var count=0;
    count++;               
    myHelper(count);
    alert(count);
}

function myHelper(count) {
    alert(count);
    count++;
}

Can I still call another method myHelper()on the other .js file? Or is there any other way that I can pass the count variable from one function to another then it will be called to other .js file. Do you have any idea regarding this one? Thanks!

我还可以myHelper()在另一个 .js 文件上调用另一个方法吗?或者有没有其他方法可以将计数变量从一个函数传递到另一个函数,然后它将被调用到其他 .js 文件。你对这个有什么想法吗?谢谢!

回答by George

Update: Nowadays you should prefer to use ES6 import/exportin a <script>tag with type="module"or via a module bundler like webpack.

更新:现在你应该更喜欢在标签中使用 ES6 import/或者通过像webpack这样的模块捆绑export<script>type="module"



When both script files are included in the same page, they run in the same global JavaScript context, so the two names will overwrite each other. So no, you can not have two functions in different .js files with the same name and access both of them as you've written it.

当两个脚本文件都包含在同一页面中时,它们运行在相同的全局 JavaScript 上下文中,因此这两个名称将相互覆盖。所以不,你不能在不同的 .js 文件中有两个具有相同名称的函数,并且不能像你写的那样访问它们。

The simplest solution would be to just rename one of the functions.

最简单的解决方案是重命名其中一个函数。

A better solution would be for you to write your JavaScript modularly with namespaces, so that each script file adds the minimum possible (preferably 1) objects to the global scope to avoid naming conflicts between separate scripts.

更好的解决方案是使用命名空间模块化编写 JavaScript,以便每个脚本文件将尽可能少的(最好是 1 个)对象添加到全局范围,以避免不同脚本之间的命名冲突。

There are a number of ways to do this in JavaScript. The simplest way is to just define a single object in each file:

在 JavaScript 中有很多方法可以做到这一点。最简单的方法是在每个文件中定义一个对象:

// In your first script file
var ModuleName = {
    myMain: function () {
        var count=0;
        count++;               
        myHelper(count);
        alert(count);
    },

    myHelper: function (count) {
        alert(count);
        count++;
    }
}

In a later script file, call the function ModuleName.myMain();

在后面的脚本文件中,调用函数 ModuleName.myMain();

A more popular method is to use a self-evaluating function, similar to the following:

一种更流行的方法是使用自评估函数,类似于以下内容:

(function (window, undefined) {

    // Your code, defining various functions, etc.
    function myMain() { ... }
    function myHelper(count) { ... }

    // More code...

    // List functions you want other scripts to access
    window.ModuleName = {
        myHelper: myHelper,
        myMain: myMain  
    };
})(window)

回答by Sabine

If you know you are about to overwrite a method, you can store the old one first in a variable and then call the other implementation of the function via that variable.

如果您知道将要覆盖一个方法,您可以先将旧方法存储在一个变量中,然后通过该变量调用该函数的另一个实现。

回答by csn

Yes, you can call myHelper() from any other js file as long as you include both js files in one html or jsp page

是的,您可以从任何其他 js 文件调用 myHelper(),只要您在一个 html 或 jsp 页面中包含这两个 js 文件

you may want to look at this : Can we call the function written in one JavaScript in another JS file?

你可能想看看这个: 我们可以在另一个 JS 文件中调用用一个 JavaScript 编写的函数吗?