Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

function overloading problem

 
Krishna Chaitanya Reddy Kuninti
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic