...

View Full Version : Handling Parameters in Object constructor call



RadarBob
06-06-2003, 10:01 PM
When I build JavaScript objects I want to make the constructor as flexible as possible.

Hypothetical example:


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).

arnyinc
06-06-2003, 10:24 PM
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.
:)

RadarBob
06-06-2003, 10:35 PM
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.

beetle
06-07-2003, 01:04 AM
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)


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.


function isNull( param )
{
return ( param == null );
}

RadarBob
06-09-2003, 02:36 PM
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.


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:


var myString = "this is a string"
typeof (myString)

typeof returns the value "string"

However if you do this:


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum