当内存中有多个同名函数时调用特定的 javascript 函数

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

call a specific javascript function when multiple same name function are in memory

javascript

提问by Tanmay

I need to call specific js function. The problem is many time runtime situation can come where another js file may contain same name function. But i need to be specific that which function i am suppose to call. Function overloading is not my solution.

我需要调用特定的 js 函数。问题是很多时候运行时可能出现另一个 js 文件可能包含同名函数的情况。但我需要具体说明我想调用哪个函数。函数重载不是我的解决方案。

Thanks and regards, Tanmay

谢谢和问候, Tanmay

回答by lincolnk

you're going to have to do some reorganization of your resources and use namespacing where you can.

您将不得不对资源进行一些重组,并尽可能使用命名空间。

if you have a method named saySomethingdefined twice, you would move one of them to an object (whichever suits your needs better).

如果你有一个被saySomething定义两次的方法,你会将其中一个移动到一个对象中(以更适合你的需要为准)。

var myNS = new (function() {

    this.saySomething = function() {
        alert('hello!');
    };

})();

and the other defintion can be moved into a different object or even left alone.

另一个定义可以移动到不同的对象中,甚至可以单独放置。

function saySomething() {
    alert('derp!');
}

you can now call the saySomethingmethod like

你现在可以saySomething像这样调用方法

saySomething();      // derp!
myNS.saySomething(); // hello!

edit: since it was brought up in comments, this

编辑:因为它是在评论中提出的,所以

var myNS = {
    saySomething: function() {
        alert('hello!');
    }
};

is equivalent to the first code block, in simpler form (if i'm remembering correctly).

等效于第一个代码块,形式更简单(如果我没记错的话)。

回答by Topera

At least in firefox, when you have two functions with the same name, the second will overwrite the first one.

至少在 Firefox 中,当你有两个同名的函数时,第二个会覆盖第一个。

So, you can't call the first one.

所以,你不能打电话给第一个。

Try it:

试试看:

function a() {alert(1);}
function a() {alert(2);}
a(); // alerts '2'

See in jsfiddle.

请参阅jsfiddle

回答by letronje

In javascript, similarly named functions automatically override previous function defined with the exact same name.

在 javascript 中,类似命名的函数会自动覆盖先前使用完全相同名称定义的函数。

Let's say your page includes 1.js and 2.js and both of them define the same function, for example say, display(). In this case, based on which js file is included the last, the definition of 'display()' in that file will override all other prior definitions.

假设您的页面包含 1.js 和 2.js,并且它们都定义了相同的函数,例如 display()。在这种情况下,根据最后包含哪个 js 文件,该文件中 'display()' 的定义将覆盖所有其他先前的定义。

回答by erosman

I use function scope to limit the scope of variables and functions

我使用函数作用域来限制变量和函数的作用域

Here is an example:

下面是一个例子:

// existing function in JavaScript
function one() {

 console.log('one');   
}

one(); // outputs one

// inserting new JavaScript
(function() { // anonymous function wrapper
'use strict'; // ECMAScript-5

   function one() {

     console.log('two');   
   } 


   one(); // outputs two

})(); // end of anonymous function

one(); // outputs one

I hope that helps :)

我希望有帮助:)