Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    function parameters question

    Hi:
    I was wondering, lets say I define a function with 3 parameters:
    Code:
    function animate(x, y, speed){
    if (speed=2)
    //do this
    }
    When I invoke it, is it officially permitted to only set some of the paramaters, like so:

    animate(20, 30)

    In the above, speed is missing, since I don't need it for this particular invocation. Is this valid and supported by functions in JavaScript (so far no errors in browsers I've tested it in)? In the past I've always passed in '' for unused parameters, though it is a sore eye.

    Thanks,
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    I think you should do a test like this instead:

    if(typeof speed!='undefined')

    to detect if speed is present. You could also ask for the length of the arguments array.

    You only need to pass '' (or null, or even better undefined (use void(0) for backward compatibility)) when you want to leave one argument out but want to set an argument after it.

    As for your if(speed=2), it's an assignment. It'll always set speed to 2, and it'll always evaluate to true. Why not do like this instead:

    var speed=typeof speed!='undefined'?speed:2; // Set speed to (the argument) speed unless that argument is undefined, when you set it to 2 instead.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Location
    Wichita
    Posts
    3,880
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The arguments passed to a function are found in an array named "arguments" and therefore the number of arguments can be found using "arguments.length". You can therefore declare a function without any explicit arguments and then retrieve them into local variables as needed (perhaps using techniques like Liorean suggests).

    Code:
    function a()
    	{
    	alert(arguments.length)
    	}
    a(1,2,3,4,5,6)
    This is very useful if you want to create a function where it can be passed a variable number of arguments like a "hide" function where you can pass it any number of objects you'd want the function to hide at the same time.
    Last edited by Roy Sinclair; 09-25-2003 at 03:04 PM.
    Check out the Forum Search. It's the short path to getting great results from this forum.

  • #4
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi,

    To expand on Roy's comments/functions:

    function a()
    {
    len = arguments.length;
    switch (len)
    {
    case 2:
    alert('Area = ' + (arguments[0] * arguments[1]) )
    break;
    case 1:
    alert('Area = ' + (3.14 * (arguments[0] * arguments[0]) ) )
    break;
    }
    }
    a(1,2)
    a(4)


    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #5
    Regular Coder
    Join Date
    Aug 2003
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    &nbsp;........ another wild guess ........

  • #6
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Yep. basic function overloading

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #7
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Not so much function overloading (for which static programming languages are needed) as dynamic functions. The determination of what to do is done inside one and the same function, and that function can only exist in one copy, though it may allow a variable number of attributes and thus act as an overloaded function would.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #8
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi liorean,

    You're right.
    Unfortunately, I tend to be a 'if it walks like a duck...' kind of guy, so for me -- it's overloading

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #9
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You should have a look at some LISPs - those where data isn't differentiated from functions and where the entire concept of data types is lost in the Right Way To Do It&trade; idea. The kind of language where there exists only three things: symbols, lists, and atoms.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #10
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    a look at some LISPs
    about 20 years ago, I fiddled with some, along with a few others -- pl/c, cobol, pascal, that one for 'real world' hookups, the turtle one for kids

    Right Way To Do Itô idea
    is that really trademarked? too much, either way


    In spite of it all -- or because of it all -- I still like to think of it as overloading

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #11
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    There's a long living standard of adding a trademark sign on The Right Way, The Wrong Way, The Best and a lot of other terms like that, so I thought it appropriate...
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #12
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Guess it is (I use quotes) and amusing as well

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #13
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    Thanks people for all the great responses. I'm aware of the arguments array (even wrote a tutorial on it: http://www.javascriptkit.com/javatutors/rfunction.shtml ), so my question is specifically if I wanted to define a function with say 3 parameters by default, how "backward compatible" is it to invoke this function with a different number of parameters (atmost 3 of course), ie:

    animate(20, 30)
    animate(20)

    Are both of the above valid in all major browsers, how these parameters are processed within the function not withstanding?
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #14
    Regular Coder Vincent Puglia's Avatar
    Join Date
    Jul 2003
    Location
    where the World once stood
    Posts
    367
    Thanks
    0
    Thanked 13 Times in 13 Posts
    Hi WA -- (George, right?),

    I've been using arguments since IE4 & NN4.5 If you define it with 3 and only deliver 1 or 2 -- make sure you handle the discrepancies -- either with switch (arguments.length) or liorean's if (typeof ..)

    Vinny
    Where the world once stood
    the blades of grass
    cut me still

  • #15
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    You can safely depend on support from JavaScript version 1.2 and on. (versions 4.0 and up of the two) I believe the typeof operator was not in the initial version of JavaScript, but I'm not entirely sure about that. As for the arguments array, it's supported earlier in nn, but added in ie4. Note that on this point, the MSDN compatibility tables don't agree with me, though.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •