Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 3 votes, 5.00 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-01-2003, 05:27 PM   PM User | #1
piz
Regular Coder

 
Join Date: Jul 2002
Location: Barcelona
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
piz is an unknown quantity at this point
overloading in JavaScript

Hi,

There is no way to overload JavaScript Functions, isn't there?

Thx.
piz
piz is offline   Reply With Quote
Old 06-01-2003, 06:11 PM   PM User | #2
shlagish
Senior Coder

 
Join Date: Apr 2003
Location: Canada
Posts: 1,063
Thanks: 2
Thanked 0 Times in 0 Posts
shlagish is an unknown quantity at this point
What's overload?
__________________
Shawn
shlagish is offline   Reply With Quote
Old 06-01-2003, 06:15 PM   PM User | #3
jkd
Senior Coder

 
jkd's Avatar
 
Join Date: May 2002
Location: metro DC
Posts: 3,163
Thanks: 1
Thanked 18 Times in 18 Posts
jkd will become famous soon enough
You can't. Javascript is loosely typed, so you can't distinguish function signatures based on datatypes of the arguments.

i.e:

int bla(int a, int b)
int bla(double a, double b)

in C++ can both be represented by

function bla(a, b)

in Javascript. As for differing amounts of arguments, Javascript really doesn't care about that either.

function bla(a, b, c)
is pretty much the same as
function bla()
except that in the former, a = arguments[0], b = arguments[1], c = arguments[2] in the namespace of the function.
__________________
jasonkarldavis.com
jkd is offline   Reply With Quote
Old 06-01-2003, 06:33 PM   PM User | #4
Garadon
Regular Coder

 
Join Date: Jul 2002
Posts: 698
Thanks: 0
Thanked 0 Times in 0 Posts
Garadon is an unknown quantity at this point
you could do a kind of fake overloading like this, it is far from ideal lol

Code:
 

<html>
<head>
<script language="JavaScript1.2">
function dispatcher()
{
  switch(arguments.length)
	{
	case 1:real1(arguments[0]);break;
  case 2:real2(arguments[0],arguments[1]);break;
	case 3:real3(arguments[0],arguments[1],arguments[2]);break;
	}
}

function real3(a,b,c)
{
  alert(a+'-'+b+'-'+c);
}

function real2(a,b)
{
  alert(a+'-'+b);
	
}

function real1(a)
{
  alert(a);
}

</script>
</head>
<body>
<button  onClick="dispatcher('a','b')">Test</button>
<button  onClick="dispatcher('a')">Test</button>
<button  onClick="dispatcher('a','b','c')">Test</button>
</body>
</html>
Garadon is offline   Reply With Quote
Old 05-21-2009, 09:00 AM   PM User | #5
nightwolfcem
New Coder

 
Join Date: Oct 2008
Location: Turkey
Posts: 15
Thanks: 0
Thanked 1 Time in 1 Post
nightwolfcem is an unknown quantity at this point
http://codingforums.com/showthread.php?t=166789
nightwolfcem is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 AM.


Advertisement
Log in to turn off these ads.