L
Laurent Bugnion, GalaSoft
Hi,
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
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