|
-
May 13th, 2004, 07:06 AM
#1
Thread Starter
Hyperactive Member
Declare in parameters in javascript function as optional?
Anyway to declare the parameters to call a function in javascript as optional?
Like: function testing(strPara1, str Para2 OPTIONAL....) {}
So that the function has to recive a number of parameters but can also recive a number of parameters that are optional...
/Smirre
Visual Basic
C, C++
Java
Access
SQL Server
MCP, MCSD
-
May 13th, 2004, 07:09 AM
#2
Frenzied Member
I don't know how to do this, but you can always make them obligatory, but make th function see when I should use them and when not to. For instance, if you want to run the function without one of the parameters, make that parameter = 0. Then the function sees that since the parameter =0, don't do a certain bit.
Have I helped you? Please Rate my posts. 
-
May 13th, 2004, 01:19 PM
#3
Thread Starter
Hyperactive Member
The parameters are optional sort of from the start.
If you have a function with say three parameters. You just specifiy the first one, the two others come out undefined, thats great. But if I need to specifiy the first and the third, or just the third parameter the function fails, not even an error is thrown...
Anyway to get by this? I guess I could throw all the values into one parameter and then seperate them inside the function, but it would have been nice to declare them optional instead....
Calling the function with 0, null or some other value is basiclly the same, as needing to specfiy each parameter.... So thats not an option....
/Smirre
Visual Basic
C, C++
Java
Access
SQL Server
MCP, MCSD
-
May 13th, 2004, 04:52 PM
#4
Use the arguments variable inside your function. It is an array of arguments passed to yourfunction.
You can use it to check the correct number of mandatory arguments have been passed and also check for additional optional arguments:
Code:
<html>
<head>
<script language="JavaScript1.1" type="text/javascript">
<!--
function test (arg1, arg2, arg3)
{
var x;
alert (arguments.length + ' arguments found.');
for (x = 0; x < arguments.length; x++)
{
alert(arguments[x]);
}
}
//-->
</script>
</head>
<body>
<script language="JavaScript1.1" type="text/javascript">
<!--
test (1,2,3,4,5,6,7,8);
//-->
</script>
</body>
</html>
-
May 14th, 2004, 02:46 AM
#5
Thread Starter
Hyperactive Member
Yeah, but that also fails.....
if I call it like this:
test (1,2,3,,5,6,7,8)
/Smirre
Visual Basic
C, C++
Java
Access
SQL Server
MCP, MCSD
-
May 14th, 2004, 11:22 AM
#6
Actually it's
Code:
function wants_more(req) {
alert(wants_more.arguments.length);
}
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 14th, 2004, 11:29 AM
#7
Originally posted by CornedBee
Actually it's
Code:
function wants_more(req) {
alert(wants_more.arguments.length);
}
Function.arguments became deprecated in some early version of Javascript and was replaced by a local function variable Arguments. I think they both still work in most browsers though.
-
May 14th, 2004, 11:39 AM
#8
True, in JS 1.4. Not that old actually, considering that 1.5 is the most recent version and I think IE doesn't support that. Or maybe since IE5.5...
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
May 14th, 2004, 11:50 AM
#9
I am quite sure it was changed in Javascript 1.1 and JScript 2.0
-
May 14th, 2004, 12:08 PM
#10
Originally posted by Smirre
Yeah, but that also fails.....
if I call it like this:
test (1,2,3,,5,6,7,8)
/Smirre
You can't call a function with arguments missing in the middle like you can with VB.
You have to have all your required arguments first and have your optional agruments in such an order that you won't have gaps in the list.
-
May 15th, 2004, 05:26 AM
#11
Thread Starter
Hyperactive Member
quote:
--------------------------------------------------------------------------------
Originally posted by Smirre
Yeah, but that also fails.....
if I call it like this:
test (1,2,3,,5,6,7,8)
/Smirre
--------------------------------------------------------------------------------
You can't call a function with arguments missing in the middle like you can with VB.
You have to have all your required arguments first and have your optional agruments in such an order that you won't have gaps in the list.
Yeah, I know that.... That was I wanted to overcome....
Visual Basic
C, C++
Java
Access
SQL Server
MCP, MCSD
-
May 15th, 2004, 02:41 PM
#12
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|