JavaScript vs. VBScript

D

Derek Richards

In VbScript, when you have a method, you do
..Method A, B, C, D. If you would take some default values, you do
..Method A,,,D

My question is that how you can do this in JavaScript
..Method(???)

Thanks.

Derek
 
L

Lasse Reichstein Nielsen

In VbScript, when you have a method, you do
.Method A, B, C, D. If you would take some default values, you do
.Method A,,,D

My question is that how you can do this in JavaScript
.Method(???)

This question was asked a few days ago. The answer still is that you
don't. In Javascript you cannot omit parameters in the middle of the list,
only from some point on.

Omitted parameters in Javascript gives the value "undefined" to the
argument variables, so you can pass "undefined" instead.

/L
 
D

Derek Richards

Here is some syntax.
expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password,
WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter,
Editable, Notify, Converter, AddToMRU)

In javascript how to omit a few parameters and at the same time,
change some default values? Say for the parrameters UpdateLinks,
ReadOnly and IgnoreReadOnlyRecommended correspondingly, I would like
to choose values 2, false and true (in javascript, is true and false
the same as 0 and 1). How to express them in javascript?
..open(capturedFile, ?,?)
 
L

Lasse Reichstein Nielsen

Here is some syntax.
expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password,
WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter,
Editable, Notify, Converter, AddToMRU)

In javascript how to omit a few parameters and at the same time,
change some default values? Say for the parrameters UpdateLinks,
ReadOnly and IgnoreReadOnlyRecommended correspondingly, I would like
to choose values 2, false and true (in javascript, is true and false
the same as 0 and 1).

No. Javascript has the boolean values true and false that are distict
from numbers. However, if used in a boolean setting, 0 and 1 convert
to the booleans false and true respectively.
How to express them in javascript? .open(capturedFile, ?,?)

expression.Open(capturedFile, 2, false, undefined, undefined, undefined, true)

This will give the value "undefined" to, e.g., Format, which is the same
value it would get if the argument is omitted.

There is no notion of default values in Javascript. An omitted argument
becomes undefined.

/L
 
D

Derek Richards

I tried the syntax you gave me. Having Visual Studio 6.0 installed, I
got a Runtime Error: Open Method of Workbooks Class Failed! My
intention was also to remove the (Read-Only) above the MenuBar on the
upper left corner when an excel file is opened.

I also changed to expression.Open(capturedFile, undefined, false,
undefined, undefined, undefined, true). Still got the same error
message.

Do you have any suggestion? Thanks.
 
S

Steve van Dongen

I tried the syntax you gave me. Having Visual Studio 6.0 installed, I
got a Runtime Error: Open Method of Workbooks Class Failed! My
intention was also to remove the (Read-Only) above the MenuBar on the
upper left corner when an excel file is opened.

I also changed to expression.Open(capturedFile, undefined, false,
undefined, undefined, undefined, true). Still got the same error
message.

Do you have any suggestion? Thanks.

You can try using null but I doubt that will work either. The only
sure way is to specify a default parameter to an ActiveX method is to
pass Nothing, and you can use the JArgUtil ActiveX object for that.
http://torrboy.customer.netspace.net.au/code/jargutil/

Regards,
Steve
 
D

Derek Richards

This is really subtle. After some changes such as changing to null,
although teh error message is gone and i specified to open in Write
mode (Read Only is false), it doesn't do the way I wished to open in
Write mode. Instead, saw (Read Only) above the MenuBar on the upper
left corner when an excel file is opened. The previously-posted syntax
is what I found under the Help in Excel's VBA Editor (you may refer to
it for details of each parameter).


Syntax

expression.Open(FileName, UpdateLinks, ReadOnly, Format, Password,
WriteResPassword, IgnoreReadOnlyRecommended, Origin, Delimiter,
Editable, Notify, Converter, AddToMRU)


UpdateLinks is supposed to be a Variant datatype in VBA. How about in
javascript (simply using undefined to take its default value)? How to
specify the parameters to open in Write mode?

Thanks very much.
 
T

Thomas 'PointedEars' Lahn

Lasse said:
[...] Javascript has the boolean values true and false that are distict
from numbers. However, if used in a boolean setting, 0 and 1 convert
to the booleans false and true respectively.

The explanation is incomplete. In Boolean expressions or expressions that
are evaluated as such (like in conditional statements) all numbers different
from 0 and -0 evaluate to `true':

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/stmtsov.html#1008323
expression.Open(capturedFile, 2, false, undefined, undefined, undefined, true)

This will give the value "undefined" to, e.g., Format, which is the same
value it would get if the argument is omitted.

Unfortunately, IE < 5.5 does not implement the `undefined'
value. However they know `window.undefined'. But I would
choose `null' here instead.
There is no notion of default values in Javascript. An omitted argument
becomes undefined.

Correct, and (JFTR) therefore you can specify default values in the function
body like

function foobar(arg1, arg2)
{
if (!arg2)
arg2 = "bla";
}

or if `arg2' is allowed to be false, 0, -0 or a reference to an object
(even `null'):

function foobar(arg1, arg2)
{
if (typeof arg2 != "undefined")
arg2 = "bla";
}


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
The explanation is incomplete. In Boolean expressions or expressions that
are evaluated as such (like in conditional statements) all numbers different
from 0 and -0 evaluate to `true':

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/stmtsov.html#1008323

Almost. The number NaN is also falsey. NaN stands for Not-a-Number,
which is (counterintuitively) a value of type number in Javascript -
typeof NaN == "number"

To be exact, the following values are converted to false by the
Boolean function and in any location that expects a boolean value (if,
while, and second argument of for-statements):

0, -0, NaN, false, "" (empty string), null, undefined

(0 and -0 are really only distinguishable internally, the programmer can't
tell the difference).

All other values are converted to true.

There is a special connection between 0/1 and true/false. If you convert
a boolean to a number, it is converted to 0 or 1.
Unfortunately, IE < 5.5 does not implement the `undefined'
value. However they know `window.undefined'.

No more than it "knows" window.arglebargle. The value of a property
access of a non-eksisting property is undefined. Which is why a simple
line can make sure that "undefined" is a defined variable:
window.undefined = window.undefined;
(browsers where window is the global object only, ofcourse).
But I would choose `null' here instead.

You could use the null value, but it is not the same as not passing
the argument. It even has a different type according to the "typeof"
operator.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
The explanation is incomplete. In Boolean expressions or expressions that
are evaluated as such (like in conditional statements) all numbers different
from 0 and -0 evaluate to `true':

http://devedge.netscape.com/library/manuals/2000/javascript/1.5/guide/stmtsov.html#1008323

Almost. The number NaN is also falsey. NaN stands for Not-a-Number,
which is (counterintuitively) a value of type number in Javascript -
typeof NaN == "number"
ACK

To be exact, [as written in the Guide]

Human gateway!!111
There is a special connection between 0/1 and true/false. If you convert
a boolean to a number, it is converted to 0 or 1.
ACK


No more than it "knows" window.arglebargle. The value of a property
access of a non-eksisting property is undefined.

Could be that it does not exist by default, I did not test it but read
about the possibility in dcljs a few days ago. It seems appropriate to
use window.undefined since `undefined' is not a reserved word and it is
unlikely that it has other meaning (in the DOM.)
Which is why a simple
line can make sure that "undefined" is a defined variable:
window.undefined = window.undefined;

Who the heck would do that???ßß
You could use the null value, but it is not the same as not passing
the argument.

I know but `undefined' is not either, since arguments.length changes
when it is used as last argument. And we are talking about arguments
in the middle here.
It even has a different type according to the "typeof" operator.

Of course, and that can be helpful.


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
Lasse Reichstein Nielsen wrote:
Could be that it does not exist by default, I did not test it but read
about the possibility in dcljs a few days ago. It seems appropriate to
use window.undefined since `undefined' is not a reserved word and it is
unlikely that it has other meaning (in the DOM.)

I don't have access to IE<6, so I can't test it. It is quite possible
that "undefined" is not a global variable (i.e., a property of the
global object, which can also be accessed through the global variable
"window"). It is the case for Netscape 3, and I think I heard it fro
some other browsers too.

It is appropriate to use "window.undefined" for a way to get an
undefined value, since it will work correctly if "undefined" is
defined too.
Who the heck would do that???ßß

Me. If I target platforms where there might not be a global "undefined"
variable, this is the simplest way to make sure there is one. If you want
it to work on the global object without assuming the name "window", you
can write
(function(){this.undefined=this.undefined;})()

If you are going to write "window.undefined" to get an undefined value,
you might as well define it once and for all. This assignment is safe
if "undefined" exists already. As you say, it is appropriate to use
window.undefined to define undefined :)
I know but `undefined' is not either, since arguments.length changes
when it is used as last argument. And we are talking about arguments
in the middle here.

If we have the function
function foo(a,b,c,d,e) { ... }
and we only want to pass it the first argument
foo(42)
then the values of b through e become undefined. That is the behavior
expected by omitting arguments, which we can do in Javascript.

In Javascript, you cannot omit arguments in the middle. Not even using
foo.apply(this,[1,,,,4]);
If you test with
"3" in arguments
then the third argument is declared and has the value undefined.
(It is browser dependent whether it is defined when you omit arguments
at the end - IE isn't, Opera and Mozilla is)

The closest you can get to omitting arguments must be to give them
the same value as if they had actually been omitted: undefined.
Of course, and that can be helpful.

That's a different point, but then the function must be written to
recognize the parameter passing convention that null represents omission,
not just the null object reference. I think it is misusing null, unless
the argument is supposed to always be an object.

/L
 
T

Thomas 'PointedEars' Lahn

Lasse said:
Thomas 'PointedEars' Lahn said:
Lasse Reichstein Nielsen wrote:

[snipped because of ACK to known information]
Of course, and that can be helpful.

That's a different point, but then the function must be written to
recognize the parameter passing convention that null represents omission,
not just the null object reference. I think it is misusing null, unless
the argument is supposed to always be an object.

Provided that the function takes the middle argument(s) as optional, meaning
that it will, to some extent, work if they have not been provided, a
conditional statement like

if (bar)
{
...
}
else if (...)
{
...
}
else
{
...
}

(where `bar' is a named argument and parts of the code can be omitted, of
course) works the same for `undefined', which is risky because it is not
backwards compatible in IE, as it works for `null' which is AFAIS
cross-browser compatible. So I do not see the point why you insist to use
risky `undefined' (even if it's saner according to the specification) when
`null' can do exactly the same.


PointedEars
 

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,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top