Chapter 10. With statements

This chapter covers

  • Why the with statement is controversial
  • How with statements work
  • Code simplification with with
  • Tricky with gotchas
  • Templating with with

The with statement is a powerful, frequently misunderstood, and controversial feature of JavaScript. A with statement allows us to put all the properties of an object within the current scope, allowing us to reference and assign to them without having to prefix them with a reference to their owning object.

It’s important to know that this statement’s continued existence within JavaScript is fleeting. The ECMAScript 5 specification prohibits its use in strict mode, to the extent of considering it a syntax error.

Moreover, the with statement, even prior to ...

Get Secrets of the JavaScript Ninja 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.