Multiple argument passed to a JavaScript Function at runtime

Hi

Today I as working with some JavaScript code and had a function where the number of parameters would change in the runtime. Actually I had to do a validation where the number of textbox to equate the value for. I wanted to pass the array of textbox to the function. 

But then I googled a bit and found a very nice feature of JavaScript which I had heard before. In JavaScript we can pass any number of parameter and get these parameter at run time.

JavaScript functions have a special property called arguments. these contain and array of the input parameters. we can use the length property of the array and loop through the parameters passed to the function. This enables the development of function where the number of parameters can change at runtime.

<SCRIPT Language="JavaScript">

function vikFunc()

{

  var arguments = vikFunc.arguments;

  for (var i = 0; i < arguments.length; i++)

            {

                alert("Argument number " + i + " value = " + arguments[i]);

            }

}

</SCRIPT> 

This function can be called with any number of arguments at the runtime like

vikFunc('Vikram', 'Lakhotia');
vikFunc('VikramLakhotia.com');

Thanks
Vikram


Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Posted on 4/27/2007 6:16:31 AM

There are more robust ways to do this. Think about if you want to modify the function later on. You are depending solely on the ordinal position of the arguments to carry out any operations. Plus, you can't check if a specific type of property is there (think about optional parameters). You can use JavaScript Object Notation to make your code more readable and maintainable:

function makeWebSite(site)
{
alert('Creating site "' + site.name + "' @ " + site.address + "...");

for(var i = 0; i < site.administrators.length; i++)
{
alert(site.administrators[i] + " has been added as user #" + (i + 1));
}
}

var newSite =
{
'name' : 'Vikram\'s Blog',
'address' : 'www.VikramLakhotia.com',
'administrators' : ['Vikram Lakhotia', 'Bill Gates']
}

makeWebSite(newSite);

Posted on 4/27/2007 8:56:40 AM

Thanks for your comment Will. I will take that into notice

Posted on 7/24/2007 11:44:27 PM

Hi vikram,

Amazing job !!!!! well done,

Can you help me in few concepts in asp.net??

jus gone thru ur profile,u have went along good career path.congrats..im new to .net and very new to AJAX tool kit.i have a question here in scripting in Addiction Pane.

how can i call my addiction Pane from Script when i have master page???

controls im calling like this
function empmas()
{
document.getElementById('<% RadioButton2.ClientID %>').disabled = true;
}
but addiction Pane how to do???help me if can...

thanks and all the best

Posted on 7/25/2007 9:54:45 AM

HI Rathi,

what do you mean by "addiction Pane"?

Posted on 11/19/2007 5:40:07 AM

Hi vikram,

Amazing job !!!!! well done.

I want to ask one question. I want to sent mail at runtime. Say after every three hrs. in ASP.NET. How can I archive this?

Posted on 11/19/2007 5:41:29 AM

Hi vikram,

Amazing job !!!!! well done,

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia