With clause syntax errors but how do I fix it?

L

Laurent Bugnion, GalaSoft

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




(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
 
L

Lasse Reichstein Nielsen

Laurent Bugnion said:
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
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
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.
 

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,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top