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 separately, a bit of a hassle, after learning the With statement, can be implemented in a similar manner as follows:
With (Objinstance)
{
var str = attribute 1;
.....
} removes the hassle of writing object names multiple times.
3) Example
<script language= "JavaScript" > <!--functionLakers () { This. Name = "Kobe Bryant"; This. Age = "28"; This. Gender = "Boy"; } varpeople=NewLakers (); with(people) {varstr = "Name:" + name + "<br>"; STR+ = "Age:" + ages + "<br>"; STR+ = "Gender:" +gender; document.write (str); } // -</script>
The code performs the following effects:
Name: Kobe Bryant
Age: 28
Sex: Boy
JavaScript provides a with statement intended to use it to quickly access the properties of an object, unfortunately, its results may sometimes be unpredictable, so you should avoid using it
For example
(obj) { B}
It is equal to one of these statements,
A == = = Obj.a
You can't tell which of these statements you get, and with itself seriously affects the speed of the JavaScript processor, it's meant to be good, but without it, the JavaScript language would be a little better.
Avoid using JS feature with (obj) {}