Bad constructs – the with statement

One example of these bad constructs is the with statement. The original intention of the with statement was to help developers access object properties without having to type the whole namespace every time. It was intended to be a sort of use statement as we might encounter them in other languages like PHP. For example, you could use the with statement in the following way:

foo.bar.baz.myVar    = false;foo.bar.baz.otherVar = false;with (foo.bar.baz) {    myVar = true;    otherVar = true;}

The problem here is that, when we are looking at this code, we are not entirely sure that the engine is not clobbering global variables named myVar and otherVar. The best way to deal with long namespaces is to assign them to local ...

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.