JavaScript With Statement

01.18.2015

JavaScript ‘with’ keyword is unreliable and expensive and should be avoided.

With statement builds a new local scope with the parameter object. Everything in object is treated as local to the ‘with’ block.

Building new properties on the fly doesn’t work as expected. It will create a global.

The goal of ‘with’ is to reduce redundancy. A better alternative is to cache the object in a variable to avoid lengthy nested object names.

Sources

https://www.codeschool.com/courses/javascript-best-practices