Results 1 to 12 of 12

Thread: Declare in parameters in javascript function as optional?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    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

  2. #2
    Frenzied Member Acidic's Avatar
    Join Date
    Sep 2003
    Location
    UK
    Posts
    1,090
    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.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    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

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343
    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

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  7. #7
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    I am quite sure it was changed in Javascript 1.1 and JScript 2.0
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  10. #10
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906
    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.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    May 2000
    Location
    Kalix, Norrbotten, SWEDEN
    Posts
    343

    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

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    You can't overcome it.
    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
  •  



Click Here to Expand Forum to Full Width