Novemberborn, Straight lines circle sometime

Named Arguments

For sIFR RC3 I wrote a handy little piece of code which allows users to use named arguments in their JavaScript code. For example, if you have a function foo which takes five arguments, and you only need to set the first and the last, this can be quite cumbersome:

function foo(one, two, three, four, five){
    // do something
};

// let's call foo() with only the first and last argument
foo("hello", null, null, null, "world");

sIFR takes thirtheen arguments, a lot of which are optional. As you can hopefully understand using named arguments is lots easier.

Interlude - For the True JavaScript Hacker

If you’re really interested in how all this all works, and how you can implement it in your own scripts… please be patient. I still have to write about the inner workings of my named arguments implementation. Sorry!

Syntax & Placement

In order for named arguments to work you need to pass a special construct to the function. The syntax of this construct is like this:

named({
    name : value,
    name : value
})

named({ is the beginning of the construct. name : value, is the syntax you need for each argument. name is the name of the argument and value is of course the value. You end the construct with }).

Due to a bug in Internet Explorer and Opera you have to be careful not to end the last argument with a comma. There will be a JavaScript error and the code will break.

By convention you pass this construct to the function as the last argument.

Example

Let’s look at the function foo from earlier in this article. Using named arguments the function call will look like this:

foo(named({one : "hello", five : "world"}));

Much easier, eh?

Here’s an example where we pass some other arguments to the function before the construct:

foo("the", "ocean", named({three : "breathes", five : "salty"}));

Here we passed on the arguments one, two, three and five to the function.

Applying it to sIFR

sIFR takes the following arguments (in this order): sSelector, sFlashSrc, sColor, sLinkColor, sHoverColor, sBgColor, nPaddingTop, nPaddingRight, nPaddingBottom, nPaddingLeft, sFlashVars, sCase, sWmode

You can find out what these arguments do in the readme.txt in the download. The names of the arguments above are the same as those which you use in the named arguments construct. For example:

sIFR.replaceElement("h1", "./vandenkeere.swf", named({sColor : "#000", sCase : "upper"}));

Here we passed on the arguments sSelector, sFlashSrc, sColor and sCase to the sIFR.replaceElement function.

Usage Ideas

There are more ways named arguments can simplify your (sIFR) life. You could also store the construct in a variable and use it as a template:

if(typeof sIFR == "function"){
    var template = named({sFlashSrc : "./vandenkeere.swf", sColor : "#000", sCase : "upper"});
    sIFR.replaceElement("h1", template);
    sIFR.replaceElement("h2", template);
};

No doubt more things are possible, but I’m leaving that to your imagination.

Download and Licensing

Just as sIFR the named arguments code is licensed under the CC-GNU LGPL. You can also download the source.

And Finally

If you have any questions about this article or the code, please contact me.

link | javascript sifr | 30 December 2004, 18:00



Novemberborn: Extra

About the author

Mark Wubben is a hacker/writer in Enschede, the Netherlands.

Read more about Mark...

Go to

Jobs (NL)

Xopus zoekt programmeurs! Verbeter de code en win!

Please donate

If you like sIFR, please consider making a donation so I can spend more time on it. Thank you.

sIFR Documentation

See the documentation for sIFR 2 and sIFR 3.

Subscribe