This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

function overloading problem

 
Krishna Chaitanya Reddy Kuninti
Greenhorn
Posts: 13
  • 0
  • 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
  • 0
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic