Visit Mozilla.org

Core JavaScript 1.5 Reference:Statements:with

From MDC


Contents

[edit] Summary

Extends the scope chain for a statement.

Statement
Implemented in: JavaScript 1.0, NES 2.0
ECMA version: ECMA-262

[edit] Syntax

with (object)
  statement

[edit] Parameters

object 
Adds the given object to the scope chain used when evaluating the statement. The parentheses around object are required.
statement 
Any statement. To execute multiple statements, use a block statement ({ ... }) to group those statements.

[edit] Description

JavaScript looks up an unqualified name by searching a scope chain associated with the execution context of the script or function containing that unqualified name. The 'with' statement adds the given object to the head of this scope chain during the evaluation of its statement body. If an unqualified name used in the body matches a property in the scope chain, then the name is bound to the property and the object containing the property. Otherwise a 'ReferenceError' is thrown.

Note: Firefox 1.5 generates a warning when the 'with' statement is used: "deprecated with statement usage". This has been removed in Firefox 1.5.0.1 (bug 322430).

Performance Pro & Con

  • Pro: 'with' can help reduce file size by reducing the need to repeat a lengthy object reference without performance penalty. The scope chain change required by 'with' is not computationally expensive. Use of 'with' will relieve the interpreter of parsing repeated object references. Note, however, that in many cases this benefit can be achieved by using a temporary variable to store a reference to the desired object.
  • Con: 'with' forces the specified object to be searched first for all unqualified name lookups. Therefore all identifiers that match formal function argument and declared local variable names will be found more slowly in a 'with' block. Where performance is important, 'with' would likely only be used to encompass code blocks that do not use function argument and declared local variable identifiers.

Ambiguity Con

  • Con: 'with' makes it hard for a human reader or JavaScript compiler to decide whether an unqualified name will be found along the scope chain, and if so, in which object. So given this example:
    function f(x, o) {
      with (o)
        print(x);
    }
    

    only when f is called is x either found or not, and if found, either in o or (if no such property exists) in f's activation object, where x names the first formal argument. If you forget to define x in the object you pass as the second argument, or if there's some similar bug or confusion, you won't get an error -- just unexpected results.

[edit] Examples

[edit] Example: Using with

The following with statement specifies that the Math object is the default object. The statements following the with statement refer to the PI property and the cos and sin methods, without specifying an object. JavaScript assumes the Math object for these references.

var a, x, y;
var r = 10;
with (Math) {
  a = PI * r * r;
  x = r * cos(PI);
  y = r * sin(PI / 2);
}