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 variables and use them afterwards:

let fBrBz = foo.bar.baz;

fBrBz.myVar = true;
fBrBz.otherVar = true;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset