File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes function overloading problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "function overloading problem" Watch "function overloading problem" New topic
Author

function overloading problem

Krishna Chaitanya Reddy Kuninti
Greenhorn

Joined: May 29, 2007
Posts: 13
Hi all,

I have written a 3 JavaScript functions with same function name.See below.

function fan()
{
alert("in first fan()")
}
function fan()
{
alert("in second fan()")
}
function fan()
{
alert("in third fan()")
}

If I try to call the function fan(), say by using onclick event then I expect that the below function will be called.


function fan()
{
alert("in first fan()")
}

But the below JavaScript function has been called.

function fan()
{
alert("in third fan()")
}


Why so?


regards,
Krishna
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Actually, the functions you have declared are actually functions of the window object, i.e. when you declare functions outside of any object's scope, they are automatically assigned to the window object.

So here, the code you have written is equivalent to reassigning the window.fan function three times, as in


Instead of calling fan(), try calling window.fan() and you'll get the same result.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: function overloading problem