Forums > Javascript

Javascript parameter strangeness

November 24, 2012 | 6:20 pm

I have posted some example js code below that is giving me trouble. The problem I am encountering is that
I have two functions with the same name, but with a different number of parameters.

function fillrgb(v1)
function fillrgb(v1, v2, v3, alpha)

When I call fillrgb(100), it actually calls the function fillrgb(v1, v2, v3, alpha) with
the unspecified parameters as 0. This renders the example rectangle invisible because
the alpha is set to 0.

Is this expected javascript behavior or is this a bug?

autowatch = 1;

var width = 500;
var height = 300;
var mgfx = new MGraphics(width,height);

// init drawing context
mgfx.init();
mgfx.relative_coords = 0;
mgfx.autofill = 0;

var outmatrix = new JitterMatrix(4, "char", width, height);
mgfx.redraw();

function bang()
{
draw();
frameCount++;
}

function draw()
{
var theImage = null;

fillrgb(100);
mgfx.rectangle(0, 0, 200, 200);
mgfx.fill();

mgfx.identity_matrix();
theImage = new Image(mgfx.pop_group());
mgfx.image_surface_draw(theImage);

theImage.tonamedmatrix(outmatrix.name);
outlet(0, "jit_matrix", outmatrix.name);

gc();

}

function fillrgb(v1)
{
mgfx.set_source_rgba(v1/255, v1/255, v1/255, 1);
}
//function fillrgb2(v1, v2, v3, alpha) < ---- but this works!
function fillrgb(v1, v2, v3, alpha)
{
mgfx.set_source_rgba(v1/255, v2/255, v3/255, alpha/255);
}


November 24, 2012 | 9:47 pm

You can’t have two functions named the same. There’s no way the computer would know which one you want to run. You might want to try testing for the number of arguments inside the code and if setting the rgba values accordingly with an if statement.


November 25, 2012 | 4:09 am

Java does not have a problem with functions with the same name. It tells
which one I want to run by the number of parameters I am using.
Does javascript not work the same way?


November 25, 2012 | 10:53 am

Unfortunately not!


November 25, 2012 | 6:36 pm

Java and C++ support method/function overloading. JavaScript does not.

Assuming Java and JavaScript behave the same way on *any* language feature, without checking if that is indeed the case, is a fairly reliable way of shooting yourself in the foot. There are a lot of subtle (and not-so-subtle) differences between the two languages.

Basically, what your code is doing is declaring an identifier "fillrgb", setting it to a function definition, then immediately redefining the identifier to point to a different function definition. By the time your code is executed, the interpreter has entirely forgotten that a single-parameter version of fillrgb() ever existed. When you call fillrgb(), you are *always* calling the triple-parameter version. If you don’t specify the second and third parameter they are, of course, set to default values.

The good news is that you *can* get the behavior of function overloading in Javascript. Read up on the arguments property of functions.


November 26, 2012 | 2:31 am

Thanks guys for the pointers. I will look into the arguments property.


Viewing 6 posts - 1 through 6 (of 6 total)