This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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
 
Consider Paul's rocket mass heater.
 
subject: function overloading problem
 
Similar Threads
Firefox unable to detect the javascript on onload event
Calling javascript function in js file from JSP
one of the defined function in Javascript not working function name alertme()
Assigning JSP variable to JavaScript
Object Expected Error