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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Handling Parameters in Object constructor call

    When I build JavaScript objects I want to make the constructor as flexible as possible.

    Hypothetical example:
    Code:
    function Thingy (thingOne, thingTwo) {
          // do stuff here
    }
    
    var myThing = new Thingy (One, Two)
    //OR
    var myThing = new Thingy (one)
    //OR
    var myThing = new Thingy ()
    //OR
    var myThing = new Thingy (, two) // valid parameter list?
    Now what I want to do is:
    * allow user to not send one or both parameters
    * default any parameters not sent
    * cleanly handle not creating the object if there are any un-resolvable problems with the parameters.

    So I guess my main question is how do I check for "non existance" of the parameter? I know I can check Thingy.parameters.length == 0 but that doesn't account for sending one or both of the parameters.

    Is it possible to not send the first parameter but send the second (in it's proper place in the parameter list - implied).

  • #2
    Regular Coder
    Join Date
    Jan 2003
    Posts
    867
    Thanks
    4
    Thanked 8 Times in 8 Posts
    I'm bad with Objects and constructors, so this might be way off.

    Can you just have a bunch of parameters and pass a null value to them whenever you don't want to use them? I know what I'm talking about is possible, but it may be absolutely useless to you.

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    An object constructor is *exactly the same* as a function with parameters. No different.

    Passing NULL is still passing a parameter. I want to be able to have an empty parameter list (for example). I know it's possible; just not sure exactly how to handle it.

    From a code design perspective, you do not want to pass NULL to "simulate" not passing a parameter. First, sometimes NULL itself is a perfectly legitimate value to pass. So if null means "no parameter here!", then what value do you pass to represent null? Second, you have to write the code (and maintain it!) for the special case / circumstance created. And hear me now and believe me later, creating "special meaning" for language constructs, like passing NULL to mean I'm really not passing any parameter at all (when actually not passing a parameter will do the trick), is asking for trouble later on. Just don't go there.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You'll just have to exercise typeof here alot.
    It would be particulary hand if you knew which parameter would be which type (ie, thingOne is a String and thingTwo is a Number)

    You can also use this in combination with a switch statment to emulate function overloading (although it's more verbose)
    Code:
    function Thingy()
    {
        switch( true )
        {
            case ( isString( arguments[0] ) && isNumber( arguments[1] ) ): // Both parameters
                // do whatever
                break;
            case ( isString( arguments[0] ) && isUndef( arguments[1] ) ):  // parameter one only
                // do whatever
                break;
            case ( isNumber( arguments[0] ) && isUndef( arguments[1] ) ):  // parameter two only
                // do whatever
                break;
            case ( isUndef( arguments[0] ) && isUndef( arguments[1] ) ):   // Neither parameter
                // do whatever
                break;
        }
    
        function isString( param )
        {
            return ( typeof param == 'String' );
        }
        function isNumber( param )
        {
            return ( typeof param == 'Number' );
        }
        function isUndef( param )
        {
            return ( typeof param == 'undefined' );
        }
    }
    I could most likely proffer a better solution if I knew more details. Otherwise - you will have to send NULL and check for that.
    Code:
    function isNull( param )
    {
        return ( param == null );
    }
    Last edited by beetle; 06-07-2003 at 12:06 AM.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It would be particulary hand if you knew which parameter would be which type (ie, thingOne is a String and thingTwo is a Number)
    I didn't 'splain very well...

    The parameter list will be defined. I'm not trying to implement a 100% generic parameter handler. I'm trying to allow the programmer to optionally leave out known parameters and let them default. A variation of this theme is to allow parameters at the beginning of the list to be defaulted (that is, to be left out) but to include other parameters.

    The parameter list will be defined, the data types are (will be) known.

    typeof & instanceof
    As for typeof, as I read my JavaScript references, typeofwill "recognize" only the following types: boolean, number, object, string, or undefined. Additionally "Because undefined was not fully implemented until JavaScript 1.3 and JScript 3.0, many supporting browers return null when passing a variable that has not been defined."

    I'm gonna use instanceof. It returns a boolean and will tell you if a variable is of *any* type, even user defined types (objects). The subtle difference: typeof tells you what type it is (w/in limits above) and instanceof will tell you (true or false) if something is of a specific type you're asking about.
    Code:
    if  (myVar instanceof myUserDefinedObjectType)
       // thats good
    else
      // handle the problem
    Interesting Quirk (added after original post)
    NOTE: following tested in IE 5.5
    If you do this:
    Code:
    var myString = "this is a string"
    typeof (myString)
    typeof returns the value "string"

    However if you do this:
    Code:
    var myString = new String ("this is a string");
    typeof (myString);
    In this case typeof returns the value "object". I suppose that makes sense; but it's not very helpful.
    Last edited by RadarBob; 06-09-2003 at 02:36 PM.


  •  

    Posting Permissions

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