The with Statement

Navigation Aids -- This Page           Navigation Aids -- This Topic           Navigation Aids -- This Site

Review of Terms Related to This Topic

Definition and Purpose

The with statement provides the means for saving time when coding references to objects. The programmer can utilize a form of shorthand when accessing objects. Consider this example without with:

document.write("The with statement can save you time"); document.write("<br />"); document.write("by using a convenient method of referencing an object"); document.write("<br />"); document.write("only once in a header."); document.write("<br />"); document.write("The subsequent statements assume the object document.write("<br />"); document.write("from the header still applies.");

The above example required the object "document" to be referenced with each write. Use the with statement where the object is referenced only once in the header

with (document) {    write("The with statement can save you time");    write("<br />");    write("by using a convenient method of referencing an object");    write("<br />");    write("only once in a header.");    write("<br />");    write("The subsequent statements assume the object    write("<br />");    write("from the header still applies."); }

Statement Syntax

with (object) {    statement(s) }

More on Syntax and Statement Behavior

with Statements and Block Statements

All statements within the with statement structure are enclosed with curly braces. This is a use of the statement block. The with statement is just one example of where the block statement may be used.

ECMA-262 Section 12.10

The with statement is an ECMAScript core statement and is detailed is section 12.10 of the ECMAScript Language Specification. Here is a partial extraction of text from this section.

The with statement adds a computed object to the front of the scope chain of the current execution context, then executes a statement with the augmented scope chain, then restores the scope chain.


Rx4AJAX        About Us | Topic Index | Contact Us | Privacy Policy | 2008 This Site Built By PPThompson