简体   繁体   English

在另一个JS文件中调用具有相同名称的函数

[英]Calling a function with same name in another JS file

I'm just a bit confused here... If I have one .js file 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? 我还可以在另一个.js文件中调用另一个方法myHelper()吗? 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. 或者有没有其他方法可以将count变量从一个函数传递到另一个函数然后它将被调用到其他.js文件。 Do you have any idea regarding this one? 你对这个有什么想法吗? Thanks! 谢谢!

Update: Nowadays you should prefer to use ES6 import / export in a <script> tag with type="module" or via a module bundler like webpack . 更新:现在你应该更喜欢在带有type="module"<script>标签中使用ES6 import / export ,或者通过webpack等模块捆绑


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. 当两个脚本文件都包含在同一页面中时,它们在相同的全局JavaScript上下文中运行,因此这两个名称将相互覆盖。 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. 所以不,你不能在具有相同名称的不同.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. 在JavaScript中有很多方法可以做到这一点。 The simplest way is to just define a single object in each file: 最简单的方法是在每个文件中定义一个对象:

// 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)

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

Yes, you can call myHelper() from any other js file as long as you include both js files in one html or jsp page 是的,只要在一个html或jsp页面中包含两个js文件,就可以从任何其他js文件调用myHelper()

you may want to look at this : Can we call the function written in one JavaScript in another JS file? 你可能想看看这个: 我们可以在另一个JS文件中调用用一个JavaScript编写的函数吗?

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

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