The with statement sets the scope of the code within a particular object.

This example shows how:

with (expression) {

   statement;

}

Take a look at the following example:

var Person = new Object();

var fname = Person.name;

var lname = Person.lastname;

var age = 35;

To avoid referring to the object instance (Person) all the time, the with statement may be used to localize the properties associated with the instance; thus making code looking like this:

with (Person) {

   var fname = name;

   var lname = lastname;

   var age = 35;

}

NOTE: However, due reported problems with the performance and debugging difficulties, the usage of with statement is generally considered a bad practice!

Read more about with statement here..

 

›› go to examples ››