Function overloading seems doesn't work

950 views
Skip to first unread message

slicker

unread,
May 7, 2009, 8:48:47 AM5/7/09
to v8-users
Hi, I'm trying to embed v8 to my app and have encounter function
overloading problem

it goes like an example below

---------------------script-----------------------------
function a(p1) {
globalVariable='1 param called'; //print globalVariable's
setter value
}
function a(p1,p2) {
globalVariable='2 param called';
}

a('1');
a('1','2');
---------------------------------------------------------
------------------------result------------------------
globalVariable='2 param called'
globalVariable='2 param called'
--------------------------------------------------------


when i run the above script, as u can see 'function a(p1,p2)' is
always called

That is, function overloading seems doesn't work but it works as
function 'overwrite'(it always call the latter one with some tests)

I'm wondering I am doing anything wrong because it works properly at
Chrome but not my embedding application.

Any comments would be helpful and thanked

Lasse R.H. Nielsen

unread,
May 7, 2009, 8:59:28 AM5/7/09
to v8-u...@googlegroups.com
This is expected behavior.
JavaScript does not have function overloading, so when you define two functions with the same name in the same scope, only the last one is accessible.
The way to get a similar functionality is to test the number of actual arguments:
--- script ---
function a(p1, p2) {
  if (arguments.length > 1) {
    globalVariable = "2 param called";
  } else {
    globalVariable = "1 param called";
  }
}
---------------
Fair warning: accessing the "arguments" object has a slight performance overhead, so if you use it in a tight loop, you might want to just test whether p2 is undefined instead:
  if (typeof p2 == "undefined") {  ...
This obviously doesn't work if the undefined value is a valid argument to the two-argument variant of the function.
Best of luck
Lasse
--
Lasse R.H. Nielsen
l...@google.com
'Faith without judgement merely degrades the spirit divine'

Alex Iskander

unread,
May 7, 2009, 8:59:28 AM5/7/09
to v8-u...@googlegroups.com
I think your issue is that JavaScript does not support function
overloading.

What your code is equivalent to is:

a = function(p1) {...};
a = function(p1, p2) {...};

The second definition replaces the value of a.

You may want to use the arguments array instead:

function a(p1)
{
if (arguments.length == 1)
globalVariable = '1 param called';
else if (arguments.length == 2)
globalVariable = '2 param called';

//or if you want to be fancy:
globalVariable = arguments.length + ' param called';
}

Alex
Alex Iskander, TPSi





Louis Santillan

unread,
May 7, 2009, 10:22:11 AM5/7/09
to v8-u...@googlegroups.com
There are several blogs/articles on method overloading. My favorite
is the one below as it also takes into account the argument types and
not just the number of arguments.

JS Method Overloading with Type Checking
http://www.thetruetribe.com/javascript/39-javascript-general/79-javascript-method-overloading

Other JS Overloading Techniques
http://ejohn.org/blog/javascript-method-overloading/
http://ajaxian.com/archives/javascript-method-overloading
http://blogger.xs4all.nl/peterned/archive/2006/12/11/160226.aspx

-Louis

slicker

unread,
May 7, 2009, 10:40:19 AM5/7/09
to v8-users
Thank you all for quick replies

Chrome just looked like work properly by chance
Reply all
Reply to author
Forward
0 new messages