The WITH statement makes it easy to reference an existing property of an object, but it cannot be used to add a property to an object, and you must explicitly reference the object if you want to create a new property for it.
Sometimes in a piece of code to use a property or method of an object more than once, you can use the WITH statement to reduce the code:
Usage: With (< OBJECT >) < statement >
Example 1:
function person () {
This.name= "Green";
This.age= "20";
}
var p = new person ();
With (p) {
var str = "name" +name+ "<br>";
str = "Age" +age;
document.write (str);
}
Example 2:
The With statement is typically used to shorten the amount of code that must be written in a particular case. In the following example, note the repeated use of Math:
x = Math.Cos (3 * Math.PI) + Math.sin (MATH.LN10);
y = Math.tan (MATH.E);
When you use the WITH statement, the code becomes shorter and easier to read:
With (Math) {
x = cos (3 * PI) + sin (LN10);
y = tan (E);
}