tags:

views:

78

answers:

3

Hi all, 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.

Thanks and regards, Tanmay

+1  A: 

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

So, you can't call the first one.

Try it:

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

See in jsfiddle.

Topera
Not only in Firefox; in *every* Javascript engine of *every* browser. You can't even do this conditionally, as the outcome will be unpredictable. E.g., see [this code](http://jsbin.com/iwifi3/edit): in several browsers (at least in Chrome and Opera), the first function declaration is always overwritten by the second, regardless of the condition.
Marcel Korpel
A: 

In javascript, same named functions automatically override earlier functions with the exact same name.

Let's say your page includes 1.js and 2.js and both of them define the same function, 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.

letronje
+2  A: 

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

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

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 saySomething method like

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

lincolnk
+1 for giving some options
Mark Schultheiss
Good answer, but why aren't you using [object literal notation](http://www.wait-till-i.com/2006/02/16/show-love-to-the-object-literal/)?
Marcel Korpel
@Marcel Korpel habit i guess. typically when i create an object there are "private" variables, which are not available with literal notation (or I don't know how to do them).
lincolnk
You're right, but I like the [YUI Module Pattern](http://yuiblog.com/blog/2007/06/12/module-pattern/) more. Especially to a newbie, a `new` object created as a singleton using a self-executing anonymous function can be overwhelming and particularly confusing. Moreover, this answers the question, but it gives way more information than needed for purposes the OP asked about. So +1 for providing a correct answer, -1 for providing a way too verbose answer (though I have to admit it's never too soon to learn good practice).
Marcel Korpel