calling a optional parameter function

D

(-: Dan :-)

hi everybody

suppose I have a function in a DLL

function F1(var1, var2, var3)

where var2 and var3 are optional parameter with default value (for
exsample "-1")

I create a new instance of my class and then I call F1.
In VBS I can call function in 4 ways:
1. F1(1)
2. F1(1,2)
3. F1(1,2,3)
4. F1(1, ,3)

but in js the #4 generate error.

Can I call 4th way?

tnx Daniele
 
R

rf

(-: Dan :-) said:
hi everybody

suppose I have a function in a DLL

function F1(var1, var2, var3)

where var2 and var3 are optional parameter with default value (for
exsample "-1")

I create a new instance of my class and then I call F1.
In VBS I can call function in 4 ways:
1. F1(1)
2. F1(1,2)
3. F1(1,2,3)
4. F1(1, ,3)

but in js the #4 generate error.

Can I call 4th way?

No.

Optional means from here on. You can not omit a parameter.

Cheers
Richard.
 
L

Lasse Reichstein Nielsen

(-: Dan :-) said:
4. F1(1, ,3)
Can I call 4th way?

Try
F1(1,undefined,3)

In Javascript, you can only omit arguments from a point. The effect
is that the parameter gets the value "undefined", just as if you had
passed it as a value.

/L
 
L

Lee

Lasse Reichstein Nielsen said:
Try
F1(1,undefined,3)

It would seem to be cleaner to supply the default value
as found in the documentation for that function:

F1(1,-1,3);
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Lasse Reichstein Nielsen said:

It would seem to be cleaner to supply the default value
as found in the documentation for that function:

F1(1,-1,3);

Since F1 may be variable, perhaps supplied as a function parameter, the
actual value used for an undefined parameter may vary. Moreover, I have
a case where undefined actually means leaving out a bit of processing.
Zero would have the same effect, but waste time.

If an undefined value is needed, then define one. I believe that var U
does this in a satisfactory manner, though a longer name might be
preferred.
 
D

(-: Dan :-)

Since F1 may be variable, perhaps supplied as a function parameter, the
actual value used for an undefined parameter may vary. Moreover, I have
a case where undefined actually means leaving out a bit of processing.
Zero would have the same effect, but waste time.

If an undefined value is needed, then define one. I believe that var U
does this in a satisfactory manner, though a longer name might be
preferred.

I can't traslate correctly......... :-((
 
L

Lasse Reichstein Nielsen

Lee said:
It would seem to be cleaner to supply the default value
as found in the documentation for that function:

F1(1,-1,3);

If you know the default value, and if there is one.

The default value for any omitted argument in Javascript *is*
undefined. However, you can only omit at the end of the argument list,
not in the middle.

A function can distinguish between
F1(1)
and
F1(1,undefined)
only by looking at arguments.length, or by checking
'1' in arguments
(although it seems to be bugged in Opera)

/L
 
L

Lasse Reichstein Nielsen

Dr John Stockton said:
If an undefined value is needed, then define one. I believe that var U
does this in a satisfactory manner, though a longer name might be
preferred.

In ECMAScript, "undefined" is a global variable holding the undefined
value.
If you target non-ECMAScript browsers, you can ensure that it is defined
with a sigle, otherwise harmless, line:
window.undefined = window.undefined;
It has the advantage of working in any scope. If you know you are at
the root scope, you can just use
var undefined;

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,522
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top