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

时间:2021-10-19 06:49:06

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? 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!

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

3 个解决方案

#1


12  

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)

#2


-3  

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.

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

#3


-4  

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编写的函数吗?

#1


12  

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)

#2


-3  

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.

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

#3


-4  

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编写的函数吗?