in JavaScript by
Why do you need to avoid with statement?

1 Answer

0 votes
by

JavaScript's with statement was intended to provide a shorthand for writing recurring accesses to objects. So it can help reduce file size by reducing the need to repeat a lengthy object reference without performance penalty. Let's take an example where it is used to avoid redundancy when accessing an object several times.

a.b.c.greeting = "welcome";
a.b.c.age = 32;

Using with it turns this into:

with (a.b.c) {
  greeting = "welcome";
  age = 32;
}

But this with statement creates performance problems since one cannot predict whether an argument will refer to a real variable or to a property inside the with argument.

...