473,480 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

With clause syntax errors but how do I fix it?

<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
22 3447
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
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
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
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
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
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
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

"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
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
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

"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
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
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
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
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
> 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
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
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
"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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
248429
by: Dave | last post by:
I have 2 tables, one with names, and another with addresses, joined by their CIVICID number (unique to the ADDRESSINFO table) in Oracle. I need to update a field in the NAMEINFO table for a...
6
9954
by: Steven An | last post by:
Howdy, I need to write an update query with multiple aggregate functions. Here is an example: UPDATE t SET t.a = ( select avg(f.q) from dbo.foo f where f.p = t.y ), t.b = ( select sum(f.q)...
21
2373
by: mollyf | last post by:
I'm creating a query, which I want to use in code in my VB.NET app. This query produces the correct results when executed in Access: SELECT tblEncounters.EncounterBeginDT, Query11.RID,...
7
3028
by: Britney | last post by:
Original code: this.oleDbSelectCommand1.CommandText = "SELECT TOP 100 user_id, password, nick_name, sex, age, has_picture, city, state, " + "country FROM dbo.users WHERE (has_picture = ?) AND (sex...
3
3082
by: Jerry | last post by:
Well, here is some weirdness. First, I noticed that I have 2 Set keywords (silly me). so I removed the 2nd "Set" but still got a syntax error. Then I removed the Where clause, and now it works...
8
2829
by: Jerry | last post by:
I am a MySQL and PHP newbie. I am having trouble getting the $w variable in my code below passed to mysql. When I use the value of $w directly in the Where clause, the correct rows are returned....
2
6914
by: Jobs | last post by:
Download the JAVA , .NET and SQL Server interview with answers Download the JAVA , .NET and SQL Server interview sheet and rate yourself. This will help you judge yourself are you really worth of...
5
2500
by: Sam | last post by:
Hi, I have one table like : MyTable {field1, field2, startdate, enddate} I want to have the count of field1 between startdate and enddate, and the count of field2 where field2 = 1 between...
4
6371
by: ryushinyama | last post by:
I had to do a lot of searching to get this one to work and in doing so I saw a lot of different sites where people were looking for this answer so I thought I would put it up. If you are trying...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
558
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.