You are previewing Secrets of the JavaScript Ninja.

Secrets of the JavaScript Ninja

Cover of Secrets of the JavaScript Ninja by John Resig and Bear Bibeault Published by Manning Publications
O'Reilly logo

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 ...

The best content for your career. Discover unlimited learning on demand for around $1/day.