By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

With clause syntax errors but how do I fix it?

P: n/a
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements[i]) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements[i].selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil
Jul 20 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Hi,

Phil Powell wrote:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements[i]) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements[i].selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil


"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameO fTheText;

myText.value = "Hello world";

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #2

P: n/a
Correction, I was wrong. See below.

Laurent Bugnion, GalaSoft wrote:
Hi,

Phil Powell wrote:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements[i]) {
if (.name == "text" || .name == "password" || .name == "textarea") {
if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements[i].selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my
only
option for help; can someone tell me what I did wrong?

Thanx
Phil

"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameO fTheText;

myText.value = "Hello world";

Laurent


OK, I was wrong. With exists in JavaScript, as described in this example:

It is often convenient to use the with statement when a section of code
uses several math constants and methods, so you don't have to type
"Math" repeatedly. For example,

with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}

So the syntax is different (you don't use the '.' like in VB).

I stand, however, by the fact that it's bad practice to use it, and I
really recommend that you don't.

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #3

P: n/a
Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements[i].options[document.alertForm.elements[i].select
edIndex].value

I think that is utterly unreadable myself! I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(

I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.

Phil

"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> wrote in
message news:3f********@news.bluewin.ch...
Correction, I was wrong. See below.

Laurent Bugnion, GalaSoft wrote:
Hi,

Phil Powell wrote:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements[i]) {
if (.name == "text" || .name == "password" || .name == "textarea") { if (.value == "") {
alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements[i].selectedIndex].value == "") {
alert("Please select a " + .name);
return false;
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and "with" does not search well on any Javascript tutorial. So this is my
only
option for help; can someone tell me what I did wrong?

Thanx
Phil

"with" works in VB, but as far as I know, it doesn't in JavaScript. It
also doesn't in most current languages. Anyway, it makes the code
unreadable, and all in all it is good practice to not use it.

If you want to simplify long or complicated structures, you can always
redeclare them:

var myText = windowNumberOne.document.theNameOfTheForm.theNameO fTheText;

myText.value = "Hello world";

Laurent


OK, I was wrong. With exists in JavaScript, as described in this example:

It is often convenient to use the with statement when a section of code
uses several math constants and methods, so you don't have to type
"Math" repeatedly. For example,

with (Math) {
a = PI * r*r
y = r*sin(theta)
x = r*cos(theta)
}

So the syntax is different (you don't use the '.' like in VB).

I stand, however, by the fact that it's bad practice to use it, and I
really recommend that you don't.

HTH,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #4

P: n/a
DU
Phil Powell wrote:
<script>
<!--

function isValidAlert() {
for (var i = 0; i < document.alertForm.length; i++) {
with (document.alertForm.elements[i]) { if (.name == "text" || .name == "password" || .name == "textarea") {
This seems suspicious. I bet it would be rather:

if (type == "text" || type == "password" || type == "textarea") {
if (.value == "")
if(value == "")

{ alert("Please enter a " + .name);
focus();
return false;
}
} else if (.name == "select-one") {
if (.options[document.alertForm.elements[i].selectedIndex].value ==
"") {
alert("Please select a " + .name);
return false;
Remove "." everywhere and try to make your code more compact.
}
}
}
}
}

//-->
</script>

I don't know the answer to this problem, I am getting syntax errors, and
"with" does not search well on any Javascript tutorial. So this is my only
option for help; can someone tell me what I did wrong?

Thanx
Phil


It is widely known that with statements are very cpu-demanding, RAM
demanding also and are best to be avoided. Even MSDN recommends this.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #5

P: n/a
DU
Phil Powell wrote:
Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements[i].options[document.alertForm.elements[i].select
edIndex].value

I think that is utterly unreadable myself!
Then just use a local variable to store each element (at the start of
your for loop). E.g.:

var IteratedFormElement = document.alertForm.elements[i];
(...)

if(IteratedFormElement.options[IteratedFormElement.selectedIndex].value
== "")
(...)

I was trying to come up with an elegant and readable solution to parse through form elements for client-side
validation. :(


[snipped]

Finally, please avoid top-posting in this newsgroup. Top-posting
destroys logical reading context and chronological order of posts in
threads.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #6

P: n/a
top-posting? HUH? English, please, or Svenska.

Phil

"DU" <dr*******@hotREMOVEmail.com> wrote in message
news:bj**********@news.eusc.inter.net...
Phil Powell wrote:
Wow, damned one way or another.. that is awful!

I don't know what is more unreadable, how you view a "with" clause, or this:
document.alertForm.elements[i].options[document.alertForm.elements[i].select edIndex].value

I think that is utterly unreadable myself!


Then just use a local variable to store each element (at the start of
your for loop). E.g.:

var IteratedFormElement = document.alertForm.elements[i];
(...)

if(IteratedFormElement.options[IteratedFormElement.selectedIndex].value
== "")
(...)

I was trying to come up with an
elegant and readable solution to parse through form elements for client-side validation. :(


[snipped]

Finally, please avoid top-posting in this newsgroup. Top-posting
destroys logical reading context and chronological order of posts in
threads.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #7

P: n/a
Top-posting is putting your reply on top of the message or phrase you are
replying to. As I'm doing now...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:swM4b.92709$xf.26491@lakeread04...
top-posting? HUH? English, please, or Svenska.

-----------------

"Phil Powell" <so*****@erols.com> schreef in bericht
news:swM4b.92709$xf.26491@lakeread04... top-posting? HUH? English, please, or Svenska.


This is the proper way of replying to a message by putting your reply
beneath the message.
JW

Jul 20 '05 #8

P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:AVL4b.92357$xf.24656@lakeread04...
I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.


No, it's not, because the scope is limited to the with clause itself. Try
the following example:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
name = "Hello 1";

function doAlert() {
var name = "Hello 2";
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
}
alert("Global var name value: " + window['name']);
alert("Local var name value: " + name);
}
</script>
</head>

<body>
<form>
<input type="text" value="text" name="test">
<input type="button" onClick="doAlert()" value="Test">
</form>
</body>
</html>

BTW, I don't subscribe to Laurent's point of view regarding the with clause.
JW

Jul 20 '05 #9

P: n/a
Ok, assuming that you have the with clause with your example. See my
comments below.

Phil

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:AVL4b.92357$xf.24656@lakeread04...
I remember you use "." in VB. Sorry but it's now

VB 1
Javascript 0

That is very unsafe to not use "." because of potential data collision.

No, it's not, because the scope is limited to the with clause itself. Try
the following example:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
name = "Hello 1";

function doAlert() {
var name = "Hello 2";
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
}
alert("Global var name value: " + window['name']);
alert("Local var name value: " + name);
}
</script>


Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB
you would have "name" and ".name".

Phil
</head>

<body>
<form>
<input type="text" value="text" name="test">
<input type="button" onClick="doAlert()" value="Test">
</form>
</body>
</html>

BTW, I don't subscribe to Laurent's point of view regarding the with clause.

JW

Jul 20 '05 #10

P: n/a
Maybe it's a comp.lang.javascript thing but I don't get it. What's wrong
with that? That is the conventional reply. I'd never think to look below
unless you direct me to look below for comments on a particular area; and I
only do that when I need to highlight something you have said.

I think DU's logic flow and mine are in different worlds.

Phil

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...
Top-posting is putting your reply on top of the message or phrase you are
replying to. As I'm doing now...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:swM4b.92709$xf.26491@lakeread04...
top-posting? HUH? English, please, or Svenska.


-----------------

"Phil Powell" <so*****@erols.com> schreef in bericht
news:swM4b.92709$xf.26491@lakeread04...
top-posting? HUH? English, please, or Svenska.


This is the proper way of replying to a message by putting your reply
beneath the message.
JW

Jul 20 '05 #11

P: n/a

"Phil Powell" <so*****@erols.com> schreef in bericht
news:HMO4b.93315$xf.73424@lakeread04...

Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB
you would have "name" and ".name".


Yes it does, but you must define the variable as a property of the function:

function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}

doAlert.name = "Hello There";

I admit, with VB the syntax is more convenient. But as you can see, there
are ways of getting there with JavaScript too.

In a way, it even forces you to think about your program design. E.g., when
my script encounters a pre-defined property 'name', then I shouldn't define
a variable with the same name. This is a good thing in my point of view.
JW

Jul 20 '05 #12

P: n/a
Never mind, you lost me completely.

Phil

"DU" <dr*******@hotREMOVEmail.com> wrote in message
news:bj**********@news.eusc.inter.net...

http://www10.brinkster.com/doctorunc...e7Section.html
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
- Resources, help and tips for Netscape 7.x users and Composer
http://www10.brinkster.com/doctorunclear/
Javascript and Browser bugs:
--
DU
top to bottom, not from bottom to top.
preferences then? Because 99.99% of people in this newsgroup read from
Are you saying that you read from bottom to top? What are your reading
>
> Phil
>
> I think DU's logic flow and mine are in different worlds.
>
> only do that when I need to highlight something you have said.
> unless you direct me to look below for comments on a particular area; and I
> with that? That is the conventional reply. I'd never think to look below > Maybe it's a comp.lang.javascript thing but I don't get it. What's

wrong Phil Powell wrote:

[snipped]



Jul 20 '05 #13

P: n/a
That is a Java/CF/PHP solution moreso than Javascript. It appears
Javascript is becoming more like Java every day!!!

I never knew you could do that!

Phil

"Janwillem Borleffs" <jw*@jwbfoto.demon.nl> wrote in message
news:3f***********************@news.euronet.nl...

"Phil Powell" <so*****@erols.com> schreef in bericht
news:HMO4b.93315$xf.73424@lakeread04...

Ok, now, what if you need both the var "name" AND
document.forms[0].elements[0].name at the same time? You see, with
Javascript not permitting the VB solution you can't do both so easily. VB you would have "name" and ".name".

Yes it does, but you must define the variable as a property of the

function:
function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}

doAlert.name = "Hello There";

I admit, with VB the syntax is more convenient. But as you can see, there
are ways of getting there with JavaScript too.

In a way, it even forces you to think about your program design. E.g., when my script encounters a pre-defined property 'name', then I shouldn't define a variable with the same name. This is a good thing in my point of view.
JW

Jul 20 '05 #14

P: n/a
On Mon, 1 Sep 2003 18:12:13 -0400, "Phil Powell" <so*****@erols.com>
wrote:
That is a Java/CF/PHP solution moreso than Javascript. It appears
Javascript is becoming more like Java every day!!!


Javascript hasn't changed in a number of years....
function doAlert() {
with (document.forms[0].elements[0]) {
alert("Form element's name value: " + name);
alert("Local var name value: " + doAlert.name);
}
}


with is considerably slower to execute and best avoided entirely.

Please learn how to post, it's not "top or bottom", it's snipped
interleaved quoting, your lack of snipping is your biggest problem.

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #15

P: n/a
JRS: In article <bj**********@news.eusc.inter.net>, seen in
news:comp.lang.javascript, DU <dr*******@hotREMOVEmail.com> posted at
Mon, 1 Sep 2003 14:23:00 :-

Finally, please avoid top-posting in this newsgroup.


The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.

Something like "responses should follow trimmed quotes" does not take
much longer to type.

Alternatively, if "top-posting" is used, the term should be defined in
the FAQ, and "; see FAQ" added. Plus, preferably, a link to the FAQ.

IMHO, javascript 'with' is a useful tool which can readily be misused.
IMHO, it can be good to use it when anyone reading the code for the
first time will, having not noticed the 'with', be able to deduce it.

with (document) {
write('This<br>')
write(That, [them])
}

with (MyDate)
document.write(getFullYear()+'/'+LZ(getMonth()+1)+
'/'+LZ(getDate()))

Where the contents of the with parentheses would be lengthy, the
alternative of using an auxiliary variable is that a new variable,
probably terse, will remain in scope until the end of the block; a human
author might reuse it without remembering to give it a new value.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> JS maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
Jul 20 '05 #16

P: n/a
> I don't know what is more unreadable, how you view a "with" clause, or this:

document.alertForm.elements[i].options[document.alertForm.elements[i].select
edIndex].value

I think that is utterly unreadable myself! I was trying to come up with an
elegant and readable solution to parse through form elements for client-side
validation. :(


The with statement should be avoided. Use a var to do what you would do with
with.

var e = document.alertForm.elements[i];
... e.options[e.selectedIndex].value ...

The var lets you be explicit, while the with can have surprising and seemingly
random behavior. The appearance of with in the language was well intentioned,
but it was a mistake and it should be removed.

http://www.crockford.com/javascript/survey.html

Jul 20 '05 #17

P: n/a
DU
Dr John Stockton wrote:
JRS: In article <bj**********@news.eusc.inter.net>, seen in
news:comp.lang.javascript, DU <dr*******@hotREMOVEmail.com> posted at
Mon, 1 Sep 2003 14:23:00 :-
Finally, please avoid top-posting in this newsgroup.

The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.


How do you figure out who is a newcomer in a newsgroup? How can you
reasonably do that?
Something like "responses should follow trimmed quotes" does not take
much longer to type.

Alternatively, if "top-posting" is used, the term should be defined in
the FAQ, and "; see FAQ" added. Plus, preferably, a link to the FAQ.


I'm all for defining "top-posting" in this newsgroup FAQ and I'm all for
explaining why top-posting just makes reading posts, replies and threads
harder for others.
Top-posting is not the natural reading direction for 99.99% of users in
this newsgroup... unless you really read from bottom to top (like
ancient Chinese).
Top-posting destroys chronological order of posts in threads.
Top-posting destroys logical reading context.
Top-posting makes others constantly scroll up and down to figure out
what was said and then what was replied: there is no book, document,
paper anywhere which goes like that. No one reads a book from end to
start. No one reads an interview in a magazine by first reading the
answers to questions. No one reads solutions first and then problem
descriptions. I don't video-tape a movie from end to start.
Top-posting generally makes it harder to understand message posts context.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #18

P: n/a
Thanx for your help.

"DU" <dr*******@hotREMOVEmail.com> wrote in message
news:bj**********@news.eusc.inter.net...
Dr John Stockton wrote:
JRS: In article <bj**********@news.eusc.inter.net>, seen in
news:comp.lang.javascript, DU <dr*******@hotREMOVEmail.com> posted at
Mon, 1 Sep 2003 14:23:00 :-
Finally, please avoid top-posting in this newsgroup.

The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.


How do you figure out who is a newcomer in a newsgroup? How can you
reasonably do that?
Something like "responses should follow trimmed quotes" does not take
much longer to type.

Alternatively, if "top-posting" is used, the term should be defined in
the FAQ, and "; see FAQ" added. Plus, preferably, a link to the FAQ.


I'm all for defining "top-posting" in this newsgroup FAQ and I'm all for
explaining why top-posting just makes reading posts, replies and threads
harder for others.
Top-posting is not the natural reading direction for 99.99% of users in
this newsgroup... unless you really read from bottom to top (like
ancient Chinese).
Top-posting destroys chronological order of posts in threads.
Top-posting destroys logical reading context.
Top-posting makes others constantly scroll up and down to figure out
what was said and then what was replied: there is no book, document,
paper anywhere which goes like that. No one reads a book from end to
start. No one reads an interview in a magazine by first reading the
answers to questions. No one reads solutions first and then problem
descriptions. I don't video-tape a movie from end to start.
Top-posting generally makes it harder to understand message posts context.

DU
--
Javascript and Browser bugs:
http://www10.brinkster.com/doctorunclear/
- Resources, help and tips for Netscape 7.x users and Composer
- Interactive demos on Popup windows, music (audio/midi) in Netscape 7.x
http://www10.brinkster.com/doctorunc...e7Section.html

Jul 20 '05 #19

P: n/a
"Douglas Crockford" <no****@laserlink.net> writes:
The var lets you be explicit, while the with can have surprising and
seemingly random behavior.
Can you give examples of deliberate use of "with" that has surpricing
and seemingly random behavior?
The appearance of with in the language
was well intentioned, but it was a mistake and it should be removed. http://www.crockford.com/javascript/survey.html


(where all you say is "The with statement should be avoided" :))
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #20

P: n/a
Hi,

Lasse Reichstein Nielsen wrote:
"Douglas Crockford" <no****@laserlink.net> writes:

The var lets you be explicit, while the with can have surprising and
seemingly random behavior.

Can you give examples of deliberate use of "with" that has surpricing
and seemingly random behavior?

The appearance of with in the language
was well intentioned, but it was a mistake and it should be removed.


http://www.crockford.com/javascript/survey.html

(where all you say is "The with statement should be avoided" :))
/L


Here is a very simple example to illustrate the problem:

var name = "Laurent Bugnion";

with ( document.formName )
{
alert( name );
}

This very simple example is already confusing, because I am not supposed
to know that there is a property called "name" in the Form object. I
cannot know that a property named like this exists in the object without
reading the API documentation.

This very simple example is not very confusing, however, because it
deals with an object and a property that we all know well. However, I
can do that with any other object, for which an API documentation might
not be available, might be more or less readable, etc...

Besides, "with" statements can be replaced by local variables, who have
all the advantages and none of the inconvenients. The readability will
be much better (you can event comment in the code why you feel the need
to replace one construct by another one, etc...).

Not even mentioning the fact that others stated, that "with" is slow,
memory intensive, etc..., I find that it complicates the code without
being useful. Since it is always good to make the readibility of your
code better, I would really recommend dropping "with" statements and
using explicit local variable declaration (and duly commented too) instead.

Hope that helps,

Laurent
--
Laurent Bugnion, GalaSoft
Webdesign, Java, javascript: http://www.galasoft-LB.ch
Private/Malaysia: http://mypage.bluewin.ch/lbugnion
Support children in Calcutta: http://www.calcutta-espoir.ch

Jul 20 '05 #21

P: n/a
"Laurent Bugnion, GalaSoft" <galasoft-LB@bluewin_NO_SPAM.ch> writes:
Here is a very simple example to illustrate the problem:

var name = "Laurent Bugnion";

with ( document.formName )
{
alert( name );
}

This very simple example is already confusing, because I am not
supposed to know that there is a property called "name" in the Form
object. I cannot know that a property named like this exists in the
object without reading the API documentation.
The point is that you should not use the "with" statement without
knowing your object. I can see that that makes it a dangerous
construction for beginners. It also means that you should either
use "with" on objects you have created yourself, and when using
it on other objects, don't access other variables directly.
This very simple example is not very confusing, however, because it
deals with an object and a property that we all know well. However, I
can do that with any other object, for which an API documentation
might not be available, might be more or less readable, etc...
Yes, know your object, or don't "with" it! :)
Besides, "with" statements can be replaced by local variables, who
have all the advantages and none of the inconvenients. The readability
will be much better (you can event comment in the code why you feel
the need to replace one construct by another one, etc...).
That is a point. There is not much saved between writing
objProperty
and
o.objProperty
Not even mentioning the fact that others stated, that "with" is slow,
memory intensive, etc...,
Blame the implementation, not the language! :)
I find that it complicates the code without being useful. Since it
is always good to make the readibility of your code better, I would
really recommend dropping "with" statements and using explicit local
variable declaration (and duly commented too) instead.


That is a much better argument. Readable code saves more developer
time than code optimizations save running time :)

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #22

P: n/a
JRS: In article <bj**********@news.eusc.inter.net>, seen in
news:comp.lang.javascript, DU <dr*******@hotREMOVEmail.com> posted at
Tue, 2 Sep 2003 13:28:06 :-
Dr John Stockton wrote:
JRS: In article <bj**********@news.eusc.inter.net>, seen in
news:comp.lang.javascript, DU <dr*******@hotREMOVEmail.com> posted at
Mon, 1 Sep 2003 14:23:00 :-
Finally, please avoid top-posting in this newsgroup.
The term "top-posting" should not be used to a newcomer, since it will
not be understood by the average innocent top-poster.


How do you figure out who is a newcomer in a newsgroup? How can you
reasonably do that?


If it is not a newcomer, then it is clearly a person who is not worth
bothering with, or a person who needs a detailed explanation. However,
one can recognise many non-newcomers by name, and many newcomers by what
they write.

--

DSS & STL.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)
Jul 20 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.