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.
1 2 |
a.b.c.greeting = 'welcome'; a.b.c.age = 32; |
Using with it turns this into:
1 2 3 4 |
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.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.