1) Brief description
the WITH statement can be conveniently used to refer to an existing property in a particular object, but cannot be used to add properties to an object. To create a new property for an object, you must explicitly refer to the object.
2) syntax format
With (object instance)
{
//code block
}
sometimes, in a program code, I need to use the properties or methods of an object many times, according to the previous wording, all through: Object. property or object. Methods to obtain the properties and methods of the object in such a way that it is a bit cumbersome to learn the WITH statement can be implemented in a similar way as follows:
With (objinstance)
{
var str = attribute 1;
.....
} removes the hassle of writing object names multiple times.
3) Example
<script language= "JavaScript" >
<!--
function Lakers () {
this.name = "Kobe Bryant";
this.age = "28";
this.gender = "Boy";
}
var people=new Lakers ();
With (people)
{
var str = "Name:" + name + "<br>";
str + = "Age:" + ages + "<br>";
str + = "Gender:" + gender;
document.write (str);
}
//-->
</script>
The code performs the following effects:
name: Kobe Bryant
Age:
Sex: Boy
How to use the With statement in JavaScript